1515package filter
1616
1717import (
18- "fmt "
18+ "bytes "
1919 "strings"
20+ "text/template"
2021 "time"
2122
2223 "github.com/getkin/kin-openapi/openapi3"
@@ -25,6 +26,31 @@ import (
2526 "golang.org/x/text/language"
2627)
2728
29+ const goSDKFormat = `import (
30+ "os"
31+ "context"
32+ sdk "go.mongodb.org/atlas-sdk/v{{ .Version }}/admin"
33+ )
34+
35+ func main() {
36+ ctx := context.Background()
37+ apiKey := os.Getenv("MONGODB_ATLAS_PUBLIC_KEY")
38+ apiSecret := os.Getenv("MONGODB_ATLAS_PRIVATE_KEY")
39+ url := os.Getenv("MONGODB_ATLAS_BASE_URL")
40+
41+ client, err := sdk.NewClient(
42+ sdk.UseDigestAuth(apiKey, apiSecret),
43+ sdk.UseBaseURL(url),
44+ sdk.UseDebug(true))
45+
46+ params = &sdk.{{ .OperationID }}ApiParams{}
47+ {{ if eq .Method "DELETE" }} httpResp, err := sdk.{{ .Tag }}Api
48+ .{{ .OperationID }}WithParams(ctx, params)
49+ .Execute(){{ else }} sdkResp, httpResp, err := sdk.{{ .Tag }}Api
50+ .{{ .OperationID }}WithParams(ctx, params)
51+ .Execute(){{ end}}
52+ }`
53+
2854const codeSampleExtensionName = "x-codeSamples"
2955
3056// https://redocly.com/docs-legacy/api-reference-docs/specification-extensions/x-code-samples#x-codesamples
@@ -125,39 +151,38 @@ func newAtlasCliCodeSamplesForOperation(op *openapi3.Operation) codeSample {
125151 }
126152}
127153
128- func (f * CodeSampleFilter ) newGoSdkCodeSamplesForOperation (op * openapi3.Operation , opMethod string ) codeSample {
154+ func (f * CodeSampleFilter ) newGoSdkCodeSamplesForOperation (op * openapi3.Operation , opMethod string ) ( codeSample , error ) {
129155 version := strings .ReplaceAll (apiVersion (f .metadata .targetVersion ), "-" , "" ) + "001"
130156 operationID := cases .Title (language .English , cases .NoLower ).String (op .OperationID )
131- sdkCall := fmt .Sprintf (
132- "sdk.%sApi\n .%sWithParams(ctx, params)\n .Execute()" ,
133- strings .ReplaceAll (op .Tags [0 ], " " , "" ), operationID )
157+ tag := strings .ReplaceAll (op .Tags [0 ], " " , "" )
134158
135- switch opMethod {
136- case "GET" , "POST" , "PATCH" , "PUT" :
137- sdkCall = " sdkResp, httpResp, err := " + sdkCall
138- case "DELETE" :
139- sdkCall = " httpResp, err := " + sdkCall
159+ t , err := template .New ("goSDK" ).Parse (goSDKFormat )
160+ if err != nil {
161+ return codeSample {}, err
140162 }
141163
142- source := "import (\n " +
143- " \" os\" \n \" context\" \n " +
144- " sdk \" go.mongodb.org/atlas-sdk/v" + version + "/admin\" \n )\n \n " +
145- "func main() {\n " +
146- " ctx := context.Background()\n " +
147- " apiKey := os.Getenv(\" MONGODB_ATLAS_PUBLIC_KEY\" )\n " +
148- " apiSecret := os.Getenv(\" MONGODB_ATLAS_PRIVATE_KEY\" )\n " +
149- " url := os.Getenv(\" MONGODB_ATLAS_BASE_URL\" )\n \n " +
150- " client, err := sdk.NewClient(\n " +
151- " sdk.UseDigestAuth(apiKey, apiSecret),\n " +
152- " sdk.UseBaseURL(url),\n " +
153- " sdk.UseDebug(true))\n \n " +
154- " params = &sdk." + operationID + "ApiParams{}\n " + sdkCall + "\n }"
164+ var buffer bytes.Buffer
165+ err = t .Execute (& buffer , struct {
166+ Tag string
167+ OperationID string
168+ Version string
169+ Method string
170+ }{
171+ Tag : tag ,
172+ OperationID : operationID ,
173+ Version : version ,
174+ Method : opMethod ,
175+ })
176+
177+ if err != nil {
178+ return codeSample {}, err
179+ }
155180
156181 return codeSample {
157182 Lang : "go" ,
158183 Label : "Go" ,
159- Source : source ,
160- }
184+ Source : buffer . String () ,
185+ }, nil
161186}
162187
163188func (f * CodeSampleFilter ) includeCodeSamplesForOperation (pathName , opMethod string , op * openapi3.Operation ) error {
@@ -174,7 +199,11 @@ func (f *CodeSampleFilter) includeCodeSamplesForOperation(pathName, opMethod str
174199 }
175200
176201 if f .metadata .targetVersion .IsStable () {
177- codeSamples = append (codeSamples , f .newGoSdkCodeSamplesForOperation (op , opMethod ))
202+ sdkSample , err := f .newGoSdkCodeSamplesForOperation (op , opMethod )
203+ if err != nil {
204+ return err
205+ }
206+ codeSamples = append (codeSamples , sdkSample )
178207 }
179208
180209 codeSamples = append (
0 commit comments