Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(go): comments are enhanced to provide better developer experience #1536

Merged
merged 3 commits into from
May 16, 2023
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
75 changes: 52 additions & 23 deletions templates/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ func HeaderParamOption(name string, val any) Option {

{{#operation}}
{{#hasParams}}
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request struct {
{{#allParams}}
{{paramName}} {{^isPathParam}}{{^isFreeFormObject}}{{^isArray}}{{^isPrimitiveType}}*{{/isPrimitiveType}}{{/isArray}}{{/isFreeFormObject}}{{/isPathParam}}{{{dataType}}}
{{/allParams}}
}

func (r *{{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) UnmarshalJSON(b []byte) error {
req := map[string]json.RawMessage{}
err := json.Unmarshal(b, &req)
Expand All @@ -70,46 +64,80 @@ func (r *{{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/
return nil
}

// {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request represents the request with all the parameters for the API call.
type {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request struct {
{{#allParams}}
{{^required}}
{{#description}}
// {{.}}
{{/description}}
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) With{{#lambda.titlecase}}{{paramName}}{{/lambda.titlecase}}({{paramName}} {{^isFreeFormObject}}{{^isArray}}{{^isPrimitiveType}}*{{/isPrimitiveType}}{{/isArray}}{{/isFreeFormObject}}{{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
r.{{paramName}} = {{paramName}}
return r
{{paramName}} {{^isPathParam}}{{^isFreeFormObject}}{{^isArray}}{{^isPrimitiveType}}*{{/isPrimitiveType}}{{/isArray}}{{/isFreeFormObject}}{{/isPathParam}}{{{dataType}}}
{{/allParams}}
}

{{/required}}
{{/allParams}}
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
//@return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request
//New{{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request creates an instance of the {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request to be used for the API call.
func (c *APIClient) NewApi{{{nickname}}}Request({{#requiredParams}} {{paramName}} {{^isPathParam}}{{^isFreeFormObject}}{{^isArray}}{{^isPrimitiveType}}*{{/isPrimitiveType}}{{/isArray}}{{/isFreeFormObject}}{{/isPathParam}}{{{dataType}}} {{^-last}},{{/-last}}{{/requiredParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
return {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request{
{{#requiredParams}}
{{paramName}}: {{paramName}},
{{/requiredParams}}
}
}

{{#allParams}}
{{^required}}
// With{{#lambda.titlecase}}{{paramName}}{{/lambda.titlecase}} adds the {{paramName}} to the {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request and returns the request for chaining.
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request) With{{#lambda.titlecase}}{{paramName}}{{/lambda.titlecase}}({{paramName}} {{^isFreeFormObject}}{{^isArray}}{{^isPrimitiveType}}*{{/isPrimitiveType}}{{/isArray}}{{/isFreeFormObject}}{{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request {
r.{{paramName}} = {{paramName}}
return r
}

{{/required}}
{{/allParams}}
{{/hasParams}}
// {{nickname}} wraps {{nickname}}WithContext using context.Background.
/*
{{operationId}} {{{summary}}} Wraps {{nickname}}WithContext using context.Background.{{^summary}}Method for {{operationId}}{{/summary}}
{{#notes}}

{{{unescapedNotes}}}
{{/notes}}

Request can be constructed by NewApi{{operationId}}Request with parameters below.
{{#allParams}}
@param {{paramName}} {{dataType}}{{#description}} - {{{.}}}{{/description}}
{{/allParams}}
{{#returnType}}@return {{{.}}}{{/returnType}}
{{#isDeprecated}}

Deprecated
{{/isDeprecated}}
*/
{{#isDeprecated}}
// Deprecated
{{/isDeprecated}}
func (c *APIClient) {{nickname}}({{#hasParams}}r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request,{{/hasParams}} opts ...Option) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}error) {
return c.{{nickname}}WithContext(context.Background(), {{#hasParams}}r,{{/hasParams}} opts...)
}

// {{{description}}}{{#returnType}}
// @return {{{.}}}{{/returnType}}
/*
{{operationId}} {{{summary}}}{{^summary}}Method for {{operationId}}{{/summary}}
{{#notes}}

{{{unescapedNotes}}}
{{/notes}}

Request can be constructed by NewApi{{operationId}}Request with parameters below.
{{#allParams}}
@param {{paramName}} {{dataType}}{{#description}} - {{{.}}}{{/description}}
{{/allParams}}
{{#returnType}}@return {{{.}}}{{/returnType}}
{{#isDeprecated}}
// Deprecated

Deprecated
{{/isDeprecated}}
*/
func (c *APIClient) {{nickname}}WithContext(ctx context.Context, {{#hasParams}}r {{#structPrefix}}{{&classname}}{{/structPrefix}}{{^structPrefix}}Api{{/structPrefix}}{{operationId}}Request,{{/hasParams}} opts ...Option) ({{#returnType}}{{^isArray}}{{^returnTypeIsPrimitive}}*{{/returnTypeIsPrimitive}}{{/isArray}}{{{.}}}, {{/returnType}}error) {
var (
postBody any
Expand Down Expand Up @@ -305,5 +333,6 @@ func (c *APIClient) {{nickname}}WithContext(ctx context.Context, {{#hasParams}}r
{{/returnType}}
return {{#returnType}}returnValue, {{/returnType}}nil
}

{{/operation}}
{{/operations}}
3 changes: 2 additions & 1 deletion templates/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type APIClient struct {
transport *transport.Transport
}

// NewClient creates a new API client with appID, apiKey and region.
// NewClient creates a new API client with {{#hasRegionalHost}}appID, apiKey and region.{{/hasRegionalHost}}{{^hasRegionalHost}}appID and apiKey.{{/hasRegionalHost}}
func NewClient(appID, apiKey string{{#hasRegionalHost}}, region Region{{/hasRegionalHost}}) *APIClient {
return NewClientWithConfig(Configuration{
AppID: appID,
Expand All @@ -50,6 +50,7 @@ return NewClientWithConfig(Configuration{
})
}

// NewClientWithConfig creates a new API client with the given configuration to fully customize the client behaviour.
func NewClientWithConfig(cfg Configuration) *APIClient {
var hosts []*transport.StatefulHost
Expand Down