diff --git a/gulpfile.js b/gulpfile.js index 94a4658e5a..f549d93782 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -140,6 +140,7 @@ var goMappings = { 'url':['../../dev/TestServer/swagger/url.json','urlgroup'], 'validation':['../../dev/TestServer/swagger/validation.json', 'validationgroup'], 'paging':['../../dev/TestServer/swagger/paging.json', 'paginggroup'], + 'more-custom-base-uri':['../../dev/TestServer/swagger/custom-baseUrl-more-options.json', 'morecustombaseurigroup'], 'azurereport':['../../dev/TestServer/swagger/azure-report.json', 'azurereport'] }; diff --git a/src/generator/AutoRest.Go.Tests/src/tests/acceptancetests/custombaseurlgrouptest/custom-baseurl_test.go b/src/generator/AutoRest.Go.Tests/src/tests/acceptancetests/custombaseurlgrouptest/custom-baseurl_test.go index 653d857174..91106cc5a8 100644 --- a/src/generator/AutoRest.Go.Tests/src/tests/acceptancetests/custombaseurlgrouptest/custom-baseurl_test.go +++ b/src/generator/AutoRest.Go.Tests/src/tests/acceptancetests/custombaseurlgrouptest/custom-baseurl_test.go @@ -5,7 +5,6 @@ import ( chk "gopkg.in/check.v1" - "tests/acceptancetests/utils" . "tests/generated/custom-baseurl" ) @@ -18,12 +17,20 @@ var _ = chk.Suite(&CustomBaseURLGroupSuite{}) var custombaseuriClient = getCustomBaseURIClient() func getCustomBaseURIClient() PathsClient { - c := NewPathsClient("local") - c.BaseURI = utils.GetBaseURI() - return c + c := NewWithoutDefaults("host:3000") + return PathsClient{ManagementClient: c} } -func (s *CustomBaseURLGroupSuite) TestGetEmpty(c *chk.C) { - _, err := custombaseuriClient.GetEmpty() +func (s *CustomBaseURLGroupSuite) TestCustomBaseUriPositive(c *chk.C) { + _, err := custombaseuriClient.GetEmpty("local") c.Assert(err, chk.IsNil) } + +func (s *CustomBaseURLGroupSuite) TestCustomBaseUriNegative(c *chk.C) { + _, err := custombaseuriClient.GetEmpty("badhost:3000") + c.Assert(err, chk.NotNil) + + custombaseuriClient.RetryAttempts = 0 + _, err = custombaseuriClient.GetEmpty("bad") + c.Assert(err, chk.NotNil) +} diff --git a/src/generator/AutoRest.Go.Tests/src/tests/acceptancetests/morecustombaseurigrouptest/more-custom-base-uri_test.go b/src/generator/AutoRest.Go.Tests/src/tests/acceptancetests/morecustombaseurigrouptest/more-custom-base-uri_test.go new file mode 100644 index 0000000000..372bdbef96 --- /dev/null +++ b/src/generator/AutoRest.Go.Tests/src/tests/acceptancetests/morecustombaseurigrouptest/more-custom-base-uri_test.go @@ -0,0 +1,27 @@ +package morecustombaseurigrouptest + +import ( + "testing" + + chk "gopkg.in/check.v1" + + . "tests/generated/more-custom-base-uri" +) + +func Test(t *testing.T) { chk.TestingT(t) } + +type MoreCustomBaseURIGroupSuite struct{} + +var _ = chk.Suite(&MoreCustomBaseURIGroupSuite{}) + +var custombaseuriClient = getMoreCustomBaseURIClient() + +func getMoreCustomBaseURIClient() PathsClient { + c := NewWithoutDefaults("test12", "host:3000") + return PathsClient{ManagementClient: c} +} + +func (s *MoreCustomBaseURIGroupSuite) TestCustomBaseUriMoreOptions(c *chk.C) { + _, err := custombaseuriClient.GetEmpty("http://lo", "cal", "key1", "v1") + c.Assert(err, chk.IsNil) +} diff --git a/src/generator/AutoRest.Go.Tests/src/tests/runner.go b/src/generator/AutoRest.Go.Tests/src/tests/runner.go index ee92b83f43..04731ee1e7 100644 --- a/src/generator/AutoRest.Go.Tests/src/tests/runner.go +++ b/src/generator/AutoRest.Go.Tests/src/tests/runner.go @@ -66,7 +66,9 @@ func runTests(allPass *bool) { "custombaseurlgroup", "filegroup", // "formdatagroup", - "paginggroup"} + "paginggroup", + "morecustombaseurigroup", + } for _, suite := range testSuites { fmt.Printf("Run test (go test ./acceptancetests/%vtest -v) ...\n", suite) diff --git a/src/generator/AutoRest.Go/CodeNamerGo.cs b/src/generator/AutoRest.Go/CodeNamerGo.cs index d1dc6b9ccd..0a90dcfd36 100644 --- a/src/generator/AutoRest.Go/CodeNamerGo.cs +++ b/src/generator/AutoRest.Go/CodeNamerGo.cs @@ -84,6 +84,7 @@ public class CodeNamerGo : CodeNamer "ManagementClient", "NewWithBaseURI", "New", + "NewWithoutDefaults", }; public IReadOnlyDictionary StatusCodeToGoString; diff --git a/src/generator/AutoRest.Go/Model/CodeModelGo.cs b/src/generator/AutoRest.Go/Model/CodeModelGo.cs index ef81fe719e..3489bf984a 100644 --- a/src/generator/AutoRest.Go/Model/CodeModelGo.cs +++ b/src/generator/AutoRest.Go/Model/CodeModelGo.cs @@ -6,6 +6,7 @@ using AutoRest.Core; using AutoRest.Core.Model; using AutoRest.Core.Utilities; +using AutoRest.Extensions; namespace AutoRest.Go.Model { @@ -48,6 +49,8 @@ public string GetDocumentation() public string BaseClient => "ManagementClient"; + public bool IsCustomBaseUri => Extensions.ContainsKey(SwaggerExtensions.ParameterizedHostExtension); + public IEnumerable ClientImports { get @@ -103,7 +106,7 @@ public string GlobalParameters var declarations = new List(); foreach (var p in Properties) { - if (!p.SerializedName.FixedValue.IsApiVersion()) + if (!p.SerializedName.FixedValue.IsApiVersion() && p.DefaultValue.FixedValue.IsNullOrEmpty()) { declarations.Add( string.Format( @@ -122,7 +125,7 @@ public string HelperGlobalParameters var invocationParams = new List(); foreach (var p in Properties) { - if (!p.SerializedName.Value.IsApiVersion()) + if (!p.SerializedName.Value.IsApiVersion() && p.DefaultValue.FixedValue.IsNullOrEmpty()) { invocationParams.Add(p.Name.Value.ToSentence()); } @@ -130,6 +133,92 @@ public string HelperGlobalParameters return string.Join(", ", invocationParams); } } + public string GlobalDefaultParameters + { + get + { + var declarations = new List(); + foreach (var p in Properties) + { + if (!p.SerializedName.FixedValue.IsApiVersion() && !p.DefaultValue.FixedValue.IsNullOrEmpty()) + { + declarations.Add( + string.Format( + (p.IsRequired || p.ModelType.CanBeEmpty() ? "{0} {1}" : "{0} *{1}"), + p.Name.Value.ToSentence(), p.ModelType.Name.Value.ToSentence())); + } + } + return string.Join(", ", declarations); + } + } + + public string HelperGlobalDefaultParameters + { + get + { + var invocationParams = new List(); + foreach (var p in Properties) + { + if (!p.SerializedName.Value.IsApiVersion() && !p.DefaultValue.FixedValue.IsNullOrEmpty()) + { + invocationParams.Add("Default" + p.Name.Value); + } + } + return string.Join(", ", invocationParams); + } + } + + public string ConstGlobalDefaultParameters + { + get + { + var constDeclaration = new List(); + foreach (var p in Properties) + { + if (!p.SerializedName.Value.IsApiVersion() && !p.DefaultValue.FixedValue.IsNullOrEmpty()) + { + constDeclaration.Add(string.Format("// Default{0} is the default value for {1}\nDefault{0} = {2}", + p.Name.Value, + p.Name.Value.ToPhrase(), + p.DefaultValue.Value)); + } + } + return string.Join("\n", constDeclaration); + } + } + + + public string AllGlobalParameters + { + get + { + if (GlobalParameters.IsNullOrEmpty()) + { + return GlobalDefaultParameters; + } + if (GlobalDefaultParameters.IsNullOrEmpty()) + { + return GlobalParameters; + } + return string.Join(", ", new string[] {GlobalParameters, GlobalDefaultParameters}); + } + } + + public string HelperAllGlobalParameters + { + get + { + if (HelperGlobalParameters.IsNullOrEmpty()) + { + return HelperGlobalDefaultParameters; + } + if (HelperGlobalDefaultParameters.IsNullOrEmpty()) + { + return HelperGlobalParameters; + } + return string.Join(", ", new string[] {HelperGlobalParameters, HelperGlobalDefaultParameters}); + } + } public IEnumerable ClientMethods { diff --git a/src/generator/AutoRest.Go/Model/MethodGo.cs b/src/generator/AutoRest.Go/Model/MethodGo.cs index b8874de48f..7941852c25 100644 --- a/src/generator/AutoRest.Go/Model/MethodGo.cs +++ b/src/generator/AutoRest.Go/Model/MethodGo.cs @@ -4,6 +4,7 @@ using AutoRest.Go.Properties; using AutoRest.Core.Utilities; using AutoRest.Core.Model; +using AutoRest.Extensions; using AutoRest.Extensions.Azure; using AutoRest.Extensions.Azure.Model; using Newtonsoft.Json; @@ -26,6 +27,9 @@ public class MethodGo : Method public bool NextAlreadyDefined { get; private set; } + public bool IsCustomBaseUri + => CodeModel.Extensions.ContainsKey(SwaggerExtensions.ParameterizedHostExtension); + public MethodGo() { NextAlreadyDefined = true; @@ -140,6 +144,10 @@ public IEnumerable LocalParameters public IEnumerable OptionalHeaderParameters => ParametersGo.HeaderParameters(false); + public IEnumerable URLParameters => ParametersGo.URLParameters(); + + public string URLMap => URLParameters.BuildParameterMap("urlParameters"); + public IEnumerable PathParameters => ParametersGo.PathParameters(); public string PathMap => PathParameters.BuildParameterMap("pathParameters"); @@ -184,7 +192,14 @@ public List PrepareDecorators } decorators.Add(HTTPMethodDecorator); - decorators.Add("autorest.WithBaseURL(client.BaseURI)"); + if (!this.IsCustomBaseUri) + { + decorators.Add(string.Format("autorest.WithBaseURL(client.BaseURI)")); + } + else + { + decorators.Add(string.Format("autorest.WithCustomBaseURL(\"{0}\", urlParameters)", CodeModel.BaseUrl)); + } decorators.Add(string.Format(PathParameters.Any() ? "autorest.WithPathParameters(\"{0}\",pathParameters)" diff --git a/src/generator/AutoRest.Go/Model/MethodGroupGo.cs b/src/generator/AutoRest.Go/Model/MethodGroupGo.cs index b51d848efd..fba11e58aa 100644 --- a/src/generator/AutoRest.Go/Model/MethodGroupGo.cs +++ b/src/generator/AutoRest.Go/Model/MethodGroupGo.cs @@ -7,6 +7,7 @@ using AutoRest.Core.Model; using AutoRest.Core.Utilities; using AutoRest.Go; +using AutoRest.Extensions; namespace AutoRest.Go.Model { @@ -17,8 +18,14 @@ public class MethodGroupGo : MethodGroup public string PackageName { get; private set; } public string BaseClient { get; private set; } + public bool IsCustomBaseUri + => CodeModel.Extensions.ContainsKey(SwaggerExtensions.ParameterizedHostExtension); + public string GlobalParameters; public string HelperGlobalParameters; + public string GlobalDefaultParameters; + public string HelperGlobalDefaultParameters; + public string ConstGlobalDefaultParameters; public IEnumerable Imports { get; private set; } public MethodGroupGo(string name): base(name) @@ -58,6 +65,9 @@ internal void Transform(CodeModelGo cmg) BaseClient = cmg.BaseClient; GlobalParameters = cmg.GlobalParameters; HelperGlobalParameters = cmg.HelperGlobalParameters; + GlobalDefaultParameters = cmg.GlobalDefaultParameters; + HelperGlobalDefaultParameters = cmg.HelperGlobalDefaultParameters; + ConstGlobalDefaultParameters = cmg.ConstGlobalDefaultParameters; diff --git a/src/generator/AutoRest.Go/Model/ParameterGo.cs b/src/generator/AutoRest.Go/Model/ParameterGo.cs index d68ed604a2..66c6687192 100644 --- a/src/generator/AutoRest.Go/Model/ParameterGo.cs +++ b/src/generator/AutoRest.Go/Model/ParameterGo.cs @@ -83,7 +83,7 @@ public string ValueForMap() return "client." + ApiVersionName; } var value = IsClientProperty - ? "client." + CodeNamerGo.Instance.PascalCase(Name.Value) + ? "client." + CodeNamerGo.Instance.GetPropertyName(Name.Value) : Name.Value; var format = IsRequired || ModelType.CanBeEmpty() @@ -186,9 +186,30 @@ public static IEnumerable HeaderParameters(this IEnumerable URLParameters(this IEnumerable parameters) + { + var urlParams = new List(); + foreach (ParameterGo p in parameters.ByLocation(ParameterLocation.Path)) + { + if (p.Method.CodeModel.BaseUrl.Contains(p.SerializedName)) + { + urlParams.Add(p); + } + } + return urlParams; + } + public static IEnumerable PathParameters(this IEnumerable parameters) { - return parameters.ByLocation(ParameterLocation.Path); + var pathParams = new List(); + foreach (ParameterGo p in parameters.ByLocation(ParameterLocation.Path)) + { + if (!p.Method.CodeModel.BaseUrl.Contains(p.SerializedName)) + { + pathParams.Add(p); + } + } + return pathParams; } public static IEnumerable QueryParameters(this IEnumerable parameters) diff --git a/src/generator/AutoRest.Go/Templates/MethodGroupTemplate.cshtml b/src/generator/AutoRest.Go/Templates/MethodGroupTemplate.cshtml index 1424340c84..fdae9ac60e 100644 --- a/src/generator/AutoRest.Go/Templates/MethodGroupTemplate.cshtml +++ b/src/generator/AutoRest.Go/Templates/MethodGroupTemplate.cshtml @@ -34,15 +34,31 @@ type @(Model.ClientName) struct { @WrapComment("// ", string.Format("New{0} creates an instance of the {0} client.", Model.ClientName)) func New@(Model.ClientName)(@(Model.GlobalParameters)) @(Model.ClientName) { - return New@(Model.ClientName)WithBaseURI(DefaultBaseURI, @(Model.HelperGlobalParameters)) + @if (!Model.IsCustomBaseUri) + { + + return New@(Model.ClientName)WithBaseURI(DefaultBaseURI, @(Model.HelperGlobalParameters)) + + } + else + { + + return @(Model.ClientName){ New(@(Model.HelperGlobalParameters))} + + } } @EmptyLine -@WrapComment("// ", string.Format("New{0}WithBaseURI creates an instance of the {0} client.", Model.ClientName)) -func New@(Model.ClientName)WithBaseURI(baseURI string, @(Model.GlobalParameters)) @(Model.ClientName) { - return @(Model.ClientName){NewWithBaseURI(baseURI, @(Model.HelperGlobalParameters))} +@if (!Model.IsCustomBaseUri) +{ + @WrapComment("// ", string.Format("New{0}WithBaseURI creates an instance of the {0} client.", Model.ClientName)) + + func New@(Model.ClientName)WithBaseURI(baseURI string, @(Model.GlobalParameters)) @(Model.ClientName) { + return @(Model.ClientName){ NewWithBaseURI(baseURI, @(Model.HelperGlobalParameters))} + } + + @EmptyLine } -@EmptyLine @foreach (var method in methods) { diff --git a/src/generator/AutoRest.Go/Templates/MethodTemplate.cshtml b/src/generator/AutoRest.Go/Templates/MethodTemplate.cshtml index c79aee2d06..78d98de871 100644 --- a/src/generator/AutoRest.Go/Templates/MethodTemplate.cshtml +++ b/src/generator/AutoRest.Go/Templates/MethodTemplate.cshtml @@ -73,6 +73,11 @@ func (client @(Model.Owner)) @(Model.MethodSignature) (@Model.MethodReturnSignat @EmptyLine // @(Model.PreparerMethodName) prepares the @(Model.Name) request. func (client @(Model.Owner)) @(Model.PreparerMethodName)(@(Model.MethodParametersSignature)) (*http.Request, error) { +@if (Model.IsCustomBaseUri && Model.URLParameters.Count() > 0) +{ + @:@(Model.URLMap) + @:@EmptyLine +} @if (Model.PathParameters.Count() > 0) { @:@(Model.PathMap) diff --git a/src/generator/AutoRest.Go/Templates/ServiceClientTemplate.cshtml b/src/generator/AutoRest.Go/Templates/ServiceClientTemplate.cshtml index 3cfa4b9073..328beb5eb5 100644 --- a/src/generator/AutoRest.Go/Templates/ServiceClientTemplate.cshtml +++ b/src/generator/AutoRest.Go/Templates/ServiceClientTemplate.cshtml @@ -28,49 +28,92 @@ import ( const ( // APIVersion is the version of the @(Model.ServiceName) APIVersion = "@Model.ApiVersion" - @EmptyLine - // DefaultBaseURI is the default URI used for the service @(Model.ServiceName) - DefaultBaseURI = "@Model.BaseUrl" + @if (!Model.IsCustomBaseUri) + { + @EmptyLine + @:// DefaultBaseURI is the default URI used for the service @(Model.ServiceName) + @:DefaultBaseURI = "@Model.BaseUrl" + } + else + { + @(Model.ConstGlobalDefaultParameters) + } ) @EmptyLine @WrapComment("// ", Model.ClientDocumentation) type @(Model.BaseClient) struct { autorest.Client - BaseURI string + @if (!Model.IsCustomBaseUri) + { + @:BaseURI string + } APIVersion string @foreach (var p in Model.Properties) { - if (!p.SerializedName.FixedValue.IsApiVersion()) - { - @:@(string.Format((p.IsRequired || p.ModelType.CanBeEmpty() - ? "{0} {1}": "{0} *{1}"), - p.Name, p.ModelType.Name)) - } + if (!p.SerializedName.FixedValue.IsApiVersion()) + { + @:@(string.Format((p.IsRequired || p.ModelType.CanBeEmpty() + ? "{0} {1}" : "{0} *{1}"), + p.Name, p.ModelType.Name)) + } } } + @EmptyLine @WrapComment("// ", string.Format("New creates an instance of the {0} client.", Model.BaseClient)) func New(@(Model.GlobalParameters))@(Model.BaseClient) { - return NewWithBaseURI(DefaultBaseURI, @(Model.HelperGlobalParameters)) + @if (!Model.IsCustomBaseUri) + { + @:return NewWithBaseURI(DefaultBaseURI, @(Model.HelperGlobalParameters)) + } + else + { + @:return NewWithoutDefaults(@(Model.HelperAllGlobalParameters)) + } } -@EmptyLine -@WrapComment("// ", string.Format("NewWithBaseURI creates an instance of the {0} client.", Model.BaseClient)) -func NewWithBaseURI(baseURI string, @(Model.GlobalParameters)) @(Model.BaseClient) { - return @(Model.BaseClient){ - Client: autorest.NewClientWithUserAgent(UserAgent()), - BaseURI: baseURI, - APIVersion: APIVersion, - @foreach (var p in Model.Properties) - { - if (!p.SerializedName.Value.IsApiVersion()) - { - @:@(string.Format("{0}: {1},", p.Name, p.Name.Value.ToSentence())) +@if (!Model.IsCustomBaseUri) +{ + + @EmptyLine + @WrapComment("// ", string.Format("NewWithBaseURI creates an instance of the {0} client.", Model.BaseClient)) + func NewWithBaseURI(baseURI string, @(Model.GlobalParameters)) @(Model.BaseClient) { + return @(Model.BaseClient){ + Client: autorest.NewClientWithUserAgent(UserAgent()), + BaseURI: baseURI, + APIVersion: APIVersion, + @foreach (var p in Model.Properties) + { + if (!p.SerializedName.Value.IsApiVersion()) + { + @:@(string.Format("{0}: {1},", p.Name, p.Name.Value.ToSentence())) + } + } } + } + +} +else +{ + + @EmptyLine + @WrapComment("// ", string.Format("NewWithoutDefaults creates an instance of the {0} client.", Model.BaseClient)) + func NewWithoutDefaults(@(Model.AllGlobalParameters)) @(Model.BaseClient) { + return @(Model.BaseClient){ + Client: autorest.NewClientWithUserAgent(UserAgent()), + APIVersion: APIVersion, + @foreach(var p in Model.Properties) + { + if (!p.SerializedName.Value.IsApiVersion()) + { + @:@(string.Format("{0}: {1},", p.Name, p.Name.Value.ToSentence())) + } + } } } + } @EmptyLine diff --git a/src/generator/AutoRest.Go/TransformerGo.cs b/src/generator/AutoRest.Go/TransformerGo.cs index b89619898d..d473087c7e 100644 --- a/src/generator/AutoRest.Go/TransformerGo.cs +++ b/src/generator/AutoRest.Go/TransformerGo.cs @@ -14,6 +14,7 @@ using System.Diagnostics; using System.Globalization; using System.Linq; +using AutoRest.Extensions.Azure; namespace AutoRest.Go { @@ -37,6 +38,7 @@ public override CodeModelGo TransformCodeModel(CodeModel cm) TransformEnumTypes(cmg); TransformMethods(cmg); TransformModelTypes(cmg); + AzureExtensions.ProcessParameterizedHost(cmg); return cmg; }