diff --git a/src/generator/AutoRest.Go/CodeNamerGo.cs b/src/generator/AutoRest.Go/CodeNamerGo.cs index 6392646879..d1dc6b9ccd 100644 --- a/src/generator/AutoRest.Go/CodeNamerGo.cs +++ b/src/generator/AutoRest.Go/CodeNamerGo.cs @@ -11,6 +11,8 @@ using AutoRest.Core.Utilities.Collections; using AutoRest.Core.Model; using AutoRest.Go.Model; +using System.Text; +using System.Text.RegularExpressions; namespace AutoRest.Go { @@ -29,7 +31,7 @@ public class CodeNamerGo : CodeNamer public virtual IEnumerable StandardImports => new string[] { "github.com/Azure/go-autorest/autorest/azure", "net/http" }; public virtual IEnumerable PageableImports => new string[] { "net/http", "github.com/Azure/go-autorest/autorest/to" }; - + public virtual IEnumerable ValidationImport => new string[] { "github.com/Azure/go-autorest/autorest/validation" }; // CommonInitialisms are those "words" within a name that Golint expects to be uppercase. @@ -86,6 +88,9 @@ public class CodeNamerGo : CodeNamer public IReadOnlyDictionary StatusCodeToGoString; + + private static readonly Regex semVerPattern = new Regex(@"^(?\d+)\.(?\d+)\.(?\d+)(?:-(?\S+))?$", RegexOptions.Compiled); + /// /// Initializes a new instance of CodeNamerGo. /// @@ -412,20 +417,12 @@ public void ReserveNamespace(string ns) // camelCase or PascalCase. private string EnsureNameCase(string name) { - List words = new List(); - new List(name.ToWords()) - .ForEach(s => - { - if (CommonInitialisms.Contains(s)) - { - words.Add(s.ToUpper()); - } - else - { - words.Add(s); - } - }); - return String.Join(String.Empty, words.ToArray()); + var builder = new StringBuilder(); + foreach (var s in name.ToWords()) + { + builder.Append(CommonInitialisms.Contains(s) ? s.ToUpper() : s); + } + return builder.ToString(); } public static string[] SDKVersionFromPackageVersion(string v) @@ -434,12 +431,19 @@ public static string[] SDKVersionFromPackageVersion(string v) { throw new ArgumentNullException("package version"); } - string[] version = v.Split('.'); - if (version.Length != 3) + + var ver = semVerPattern.Match(v); + + if (ver.Success) { - throw new InvalidOperationException("version string should have major, minor and patch versions."); + var tagVal = ver.Groups["tag"].Success ? ver.Groups["tag"].Value : ""; + return new[] { ver.Groups["major"].Value, ver.Groups["minor"].Value, ver.Groups["patch"].Value, tagVal }; + } - return version; + throw new ArgumentException( + paramName: nameof(v), + message: "Version strings should be either of the format \"..\" or \"..-\". Where major, minor, and patch are decimal numbers and tag does not include whitespace."); + } public override string EscapeDefaultValue(string defaultValue, IModelType type) diff --git a/src/generator/AutoRest.Go/Templates/VersionTemplate.cshtml b/src/generator/AutoRest.Go/Templates/VersionTemplate.cshtml index 6ba8e4957b..4552319b0b 100644 --- a/src/generator/AutoRest.Go/Templates/VersionTemplate.cshtml +++ b/src/generator/AutoRest.Go/Templates/VersionTemplate.cshtml @@ -13,7 +13,9 @@ package @Model.Namespace @EmptyLine import ( + "bytes" "fmt" + "strings" ) @EmptyLine @@ -21,21 +23,36 @@ const ( major = "@(Model.Version[0])" minor = "@(Model.Version[1])" patch = "@(Model.Version[2])" - // Always begin a "tag" with a dash (as per http://semver.org) - tag = "-beta" + tag = "@(Model.Version[3])" - semVerFormat = "%s.%s.%s%s" userAgentFormat = "Azure-SDK-For-Go/%s arm-%s/%s" ) +// cached results of UserAgent and Version to prevent repeated operations. +var ( + userAgent string + version string +) + @EmptyLine // UserAgent returns the UserAgent string to use when sending http.Requests. func UserAgent() string { - return fmt.Sprintf(userAgentFormat, Version(), "@(Model.Namespace)", "@(Model.ApiVersion)") + if userAgent == "" { + userAgent = fmt.Sprintf(userAgentFormat, Version(), "@(Model.Namespace)", "@(Model.ApiVersion)") + } + return userAgent } @EmptyLine // Version returns the semantic version (see http://semver.org) of the client. func Version() string { - return fmt.Sprintf(semVerFormat, major, minor, patch, tag) + if version == "" { + versionBuilder := bytes.NewBufferString(fmt.Sprintf("%d.%d.%d", major, minor, patch)) + if tag != "" { + versionBuilder.WriteRune('-') + versionBuilder.WriteString(strings.TrimPrefix(tag, "-")) + } + version = string(versionBuilder.Bytes()) + } + return version }