Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions src/generator/AutoRest.Go/CodeNamerGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -29,7 +31,7 @@ public class CodeNamerGo : CodeNamer
public virtual IEnumerable<string> StandardImports => new string[] { "github.com/Azure/go-autorest/autorest/azure", "net/http" };

public virtual IEnumerable<string> PageableImports => new string[] { "net/http", "github.com/Azure/go-autorest/autorest/to" };

public virtual IEnumerable<string> ValidationImport => new string[] { "github.com/Azure/go-autorest/autorest/validation" };

// CommonInitialisms are those "words" within a name that Golint expects to be uppercase.
Expand Down Expand Up @@ -86,6 +88,9 @@ public class CodeNamerGo : CodeNamer

public IReadOnlyDictionary<HttpStatusCode, string> StatusCodeToGoString;


private static readonly Regex semVerPattern = new Regex(@"^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-(?<tag>\S+))?$", RegexOptions.Compiled);

/// <summary>
/// Initializes a new instance of CodeNamerGo.
/// </summary>
Expand Down Expand Up @@ -412,20 +417,12 @@ public void ReserveNamespace(string ns)
// camelCase or PascalCase.
private string EnsureNameCase(string name)
{
List<string> words = new List<string>();
new List<string>(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)
Expand All @@ -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 \"<major>.<minor>.<patch>\" or \"<major>.<minor>.<patch>-<tag>\". Where major, minor, and patch are decimal numbers and tag does not include whitespace.");

}

public override string EscapeDefaultValue(string defaultValue, IModelType type)
Expand Down
27 changes: 22 additions & 5 deletions src/generator/AutoRest.Go/Templates/VersionTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,46 @@ package @Model.Namespace

@EmptyLine
import (
"bytes"
"fmt"
"strings"
)
@EmptyLine

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
}