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
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

chk "gopkg.in/check.v1"

"tests/acceptancetests/utils"
. "tests/generated/custom-baseurl"
)

Expand All @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 3 additions & 1 deletion src/generator/AutoRest.Go.Tests/src/tests/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/generator/AutoRest.Go/CodeNamerGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class CodeNamerGo : CodeNamer
"ManagementClient",
"NewWithBaseURI",
"New",
"NewWithoutDefaults",
};

public IReadOnlyDictionary<HttpStatusCode, string> StatusCodeToGoString;
Expand Down
93 changes: 91 additions & 2 deletions src/generator/AutoRest.Go/Model/CodeModelGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AutoRest.Core;
using AutoRest.Core.Model;
using AutoRest.Core.Utilities;
using AutoRest.Extensions;

namespace AutoRest.Go.Model
{
Expand Down Expand Up @@ -48,6 +49,8 @@ public string GetDocumentation()

public string BaseClient => "ManagementClient";

public bool IsCustomBaseUri => Extensions.ContainsKey(SwaggerExtensions.ParameterizedHostExtension);

public IEnumerable<string> ClientImports
{
get
Expand Down Expand Up @@ -103,7 +106,7 @@ public string GlobalParameters
var declarations = new List<string>();
foreach (var p in Properties)
{
if (!p.SerializedName.FixedValue.IsApiVersion())
if (!p.SerializedName.FixedValue.IsApiVersion() && p.DefaultValue.FixedValue.IsNullOrEmpty())
{
declarations.Add(
string.Format(
Expand All @@ -122,14 +125,100 @@ public string HelperGlobalParameters
var invocationParams = new List<string>();
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());
}
}
return string.Join(", ", invocationParams);
}
}
public string GlobalDefaultParameters
{
get
{
var declarations = new List<string>();
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<string>();
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<string>();
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<MethodGo> ClientMethods
{
Expand Down
17 changes: 16 additions & 1 deletion src/generator/AutoRest.Go/Model/MethodGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -140,6 +144,10 @@ public IEnumerable<ParameterGo> LocalParameters

public IEnumerable<ParameterGo> OptionalHeaderParameters => ParametersGo.HeaderParameters(false);

public IEnumerable<ParameterGo> URLParameters => ParametersGo.URLParameters();

public string URLMap => URLParameters.BuildParameterMap("urlParameters");

public IEnumerable<ParameterGo> PathParameters => ParametersGo.PathParameters();

public string PathMap => PathParameters.BuildParameterMap("pathParameters");
Expand Down Expand Up @@ -184,7 +192,14 @@ public List<string> 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)"
Expand Down
10 changes: 10 additions & 0 deletions src/generator/AutoRest.Go/Model/MethodGroupGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using AutoRest.Core.Model;
using AutoRest.Core.Utilities;
using AutoRest.Go;
using AutoRest.Extensions;

namespace AutoRest.Go.Model
{
Expand All @@ -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<string> Imports { get; private set; }

public MethodGroupGo(string name): base(name)
Expand Down Expand Up @@ -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;



Expand Down
25 changes: 23 additions & 2 deletions src/generator/AutoRest.Go/Model/ParameterGo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -186,9 +186,30 @@ public static IEnumerable<ParameterGo> HeaderParameters(this IEnumerable<Paramet
return parameters.ByLocationAsRequired(ParameterLocation.Header, isRequired);
}

public static IEnumerable<ParameterGo> URLParameters(this IEnumerable<ParameterGo> parameters)
{
var urlParams = new List<ParameterGo>();
foreach (ParameterGo p in parameters.ByLocation(ParameterLocation.Path))
{
if (p.Method.CodeModel.BaseUrl.Contains(p.SerializedName))
{
urlParams.Add(p);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[syntax sugar opportunity]
Instead of allocating urlParams you could rewrite this function to look like:

public static IEnumerable<ParameterGo> URLParameters(this IEnumerable<ParameterGo> parameters)
{
    foreach (ParameterGo p in parameters.ByLocation(ParameterLocation.Path))
    {
        if (p.Method.CodeModel.BaseUrl.Contains(p.SerializedName))
        {
            yield return p;
        }
    }
}

Alternatively, you could embrace LINQ:

public static IEnumerable<ParameterGo> URLParameters(this IEnumerable<ParameterGo> parameters)
{
    return parameters.ByLocation(ParameterLocation.Path).Where(p => p.Method.CodeModel.BaseUrl.Contains(p.SerializedName));
}

Needless to say, these suggestions aren't even nits, just showing off cool things about C# :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

}
}
return urlParams;
}

public static IEnumerable<ParameterGo> PathParameters(this IEnumerable<ParameterGo> parameters)
{
return parameters.ByLocation(ParameterLocation.Path);
var pathParams = new List<ParameterGo>();
foreach (ParameterGo p in parameters.ByLocation(ParameterLocation.Path))
{
if (!p.Method.CodeModel.BaseUrl.Contains(p.SerializedName))
{
pathParams.Add(p);
}
}
return pathParams;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above would apply here as well.

}

public static IEnumerable<ParameterGo> QueryParameters(this IEnumerable<ParameterGo> parameters)
Expand Down
26 changes: 21 additions & 5 deletions src/generator/AutoRest.Go/Templates/MethodGroupTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
<text>
return New@(Model.ClientName)WithBaseURI(DefaultBaseURI, @(Model.HelperGlobalParameters))
</text>
}
else
{
<text>
return @(Model.ClientName){ New(@(Model.HelperGlobalParameters))}
</text>
}
}
@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))
<text>
func New@(Model.ClientName)WithBaseURI(baseURI string, @(Model.GlobalParameters)) @(Model.ClientName) {
return @(Model.ClientName){ NewWithBaseURI(baseURI, @(Model.HelperGlobalParameters))}
}
</text>
@EmptyLine
}
@EmptyLine

@foreach (var method in methods)
{
Expand Down
5 changes: 5 additions & 0 deletions src/generator/AutoRest.Go/Templates/MethodTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading