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): Simplify generated code #5213

Merged
merged 4 commits into from
Nov 20, 2024
Merged

feat(go): Simplify generated code #5213

merged 4 commits into from
Nov 20, 2024

Conversation

amckinney
Copy link
Member

@amckinney amckinney commented Nov 19, 2024

This adapts generated endpoint structure to clean up the generated code and reduce repetition. This also proposes a simpler errorDecoder constructor, splits specific method invocations across multiple lines, and removes a variety of unnecessary newlines across the SDK.

Now, users are presented with an endpoint that looks something along the lines of the following:

func (c *Client) Get(
	ctx context.Context,
	id string,
	opts ...option.RequestOption,
) (string, error) {
	options := core.NewRequestOptions(opts...)
	baseURL := internal.ResolveBaseURL(
		options.BaseURL,
		c.baseURL,
		"https://api.foo.io/v1",
	)
	endpointURL := internal.EncodeURL(
		baseURL+"/%v",
		id,
	)
	headers := internal.MergeHeaders(
		c.header.Clone(),
		options.ToHeader(),
	)
	errorCodes := map[int]func() error{
		400: func(apiError *core.APIError) error {
			return &acme.UntypedError{
                         	APIError: apiError,
                         }
		},
		404: func(apiError *core.APIError) error {
			return &acme.NotFoundError{
                         	APIError: apiError,
                         }
		},
	}

	var response string
	if err := c.caller.Call(
		ctx,
		&internal.CallParams{
			URL:             endpointURL,
			Method:          http.MethodGet,
			MaxAttempts:     options.MaxAttempts,
			Headers:         headers,
			BodyProperties:  options.BodyProperties,
			QueryParameters: options.QueryParameters,
			Client:          options.HTTPClient,
			Response:        &response,
			ErrorDecoder:    internal.NewErrorDecoder(errorCodes),
		},
	); err != nil {
		return "", err
	}
	return response, nil
}

Copy link

github-actions bot commented Nov 19, 2024

@amckinney amckinney marked this pull request as ready for review November 20, 2024 15:43
@amckinney amckinney requested a review from dsinghvi as a code owner November 20, 2024 15:43
@amckinney amckinney changed the title feat(go): [WIP] Simplify generated code feat(go): Simplify generated code Nov 20, 2024
@amckinney amckinney merged commit cc8f9cd into main Nov 20, 2024
64 checks passed
@amckinney amckinney deleted the amckinney/go/url branch November 20, 2024 15:52
fern-support pushed a commit that referenced this pull request Nov 20, 2024
eyw520 added a commit that referenced this pull request Nov 20, 2024
…ethod names. (#5233)

* Implement PHP SDK short-term solution.

* Update versions for both cli and php generator.

* feat(typescript): introduce API Promise so that we can return raw response headers (#5218)

* feat(typescript): RequestOptions takes in arbitrary headers (#5228)

* feat(go): Simplify generated code (#5213)

* feat(csharp): Make JsonSerializerOptions more configurable  (#5227)

Provide partial method ConfigureJsonSerializerOptions to let SDK maintainers customize JsonSerializerOptions

* chore: update changelog

* Define ENDPOINT_OVERRIDE_KEYWORDS in constants.ts

* Update naming.

---------

Co-authored-by: Deep Singhvi <deep@buildwithfern.com>
Co-authored-by: Alex McKinney <alexmckinney01@gmail.com>
Co-authored-by: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com>
Co-authored-by: eyw520 <eyw520@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants