-
Notifications
You must be signed in to change notification settings - Fork 14
feat: autogenerate go SDK example in the codeSample #772
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
Changes from all commits
2e2506f
bbb28f3
fc96f27
21f4529
d5209e0
44252e6
6ef6732
adbd960
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,47 @@ | |
package filter | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
goFormat "go/format" | ||
"strings" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/mongodb/openapi/tools/cli/internal/apiversion" | ||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
const goSDKTemplate = `import ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [follow up] conmsider creating a |
||
"os" | ||
"context" | ||
"log" | ||
sdk "go.mongodb.org/atlas-sdk/v{{ .Version }}/admin" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
clientID := os.Getenv("MONGODB_ATLAS_CLIENT_ID") | ||
clientSecret := os.Getenv("MONGODB_ATLAS_CLIENT_SECRET") | ||
|
||
client, err := sdk.NewClient( | ||
sdk.UseOAuthAuth(clientID, clientSecret), | ||
sdk.UseBaseURL(url)) | ||
|
||
andreaangiolillo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Fatalf("Error: %v", err) | ||
} | ||
|
||
params = &sdk.{{ .OperationID }}ApiParams{} | ||
{{ if eq .Method "DELETE" }} httpResp, err := client.{{ .Tag }}Api. | ||
{{ .OperationID }}WithParams(ctx, params). | ||
Execute(){{ else }} sdkResp, httpResp, err := client.{{ .Tag }}Api. | ||
{{ .OperationID }}WithParams(ctx, params). | ||
Execute(){{ end}} | ||
}` | ||
|
||
const codeSampleExtensionName = "x-codeSamples" | ||
|
||
// https://redocly.com/docs-legacy/api-reference-docs/specification-extensions/x-code-samples#x-codesamples | ||
|
@@ -121,6 +156,47 @@ func newAtlasCliCodeSamplesForOperation(op *openapi3.Operation) codeSample { | |
} | ||
} | ||
|
||
func (f *CodeSampleFilter) newGoSdkCodeSamplesForOperation(op *openapi3.Operation, opMethod string) (*codeSample, error) { | ||
version := strings.ReplaceAll(apiVersion(f.metadata.targetVersion), "-", "") + "001" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field represents the SDK version. The value is formatted as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit curious on how this version stays in sync with the latest SDK version:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, neither of those options apply. I generate the SDK version by combining the API version in the format |
||
operationID := cases.Title(language.English, cases.NoLower).String(op.OperationID) | ||
tag := strings.ReplaceAll(op.Tags[0], " ", "") | ||
tag = strings.ReplaceAll(tag, ".", "") | ||
|
||
t, err := template.New("goSDK").Parse(goSDKTemplate) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var buffer bytes.Buffer | ||
err = t.Execute(&buffer, struct { | ||
Tag string | ||
OperationID string | ||
Version string | ||
Method string | ||
}{ | ||
Tag: tag, | ||
OperationID: operationID, | ||
Version: version, | ||
Method: opMethod, | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
formattedResult, err := goFormat.Source(buffer.Bytes()) | ||
if err != nil { | ||
return nil, fmt.Errorf("tag: %q, operationId: %q code: %q: error: %w", | ||
op.Tags[0], operationID, buffer.String(), err) | ||
} | ||
|
||
return &codeSample{ | ||
Lang: "go", | ||
Label: "Go", | ||
Source: string(formattedResult), | ||
}, nil | ||
} | ||
|
||
func (f *CodeSampleFilter) includeCodeSamplesForOperation(pathName, opMethod string, op *openapi3.Operation) error { | ||
if op == nil || opMethod == "" || pathName == "" { | ||
return nil | ||
|
@@ -130,10 +206,22 @@ func (f *CodeSampleFilter) includeCodeSamplesForOperation(pathName, opMethod str | |
op.Extensions = map[string]any{} | ||
} | ||
|
||
op.Extensions[codeSampleExtensionName] = []codeSample{ | ||
codeSamples := []codeSample{ | ||
newAtlasCliCodeSamplesForOperation(op), | ||
f.newServiceAccountCurlCodeSamplesForOperation(pathName, opMethod), | ||
f.newDigestCurlCodeSamplesForOperation(pathName, opMethod), | ||
} | ||
|
||
if f.metadata.targetVersion.IsStable() { | ||
sdkSample, err := f.newGoSdkCodeSamplesForOperation(op, opMethod) | ||
if err != nil { | ||
return err | ||
} | ||
codeSamples = append(codeSamples, *sdkSample) | ||
} | ||
|
||
codeSamples = append( | ||
codeSamples, | ||
f.newServiceAccountCurlCodeSamplesForOperation(pathName, opMethod), | ||
f.newDigestCurlCodeSamplesForOperation(pathName, opMethod)) | ||
op.Extensions[codeSampleExtensionName] = codeSamples | ||
return nil | ||
} |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main changes were in this file. The files under tests were autogenerated.