1515package filter
1616
1717import (
18+ "fmt"
19+ "strings"
1820 "time"
1921
2022 "github.com/getkin/kin-openapi/openapi3"
2123 "github.com/mongodb/openapi/tools/cli/internal/apiversion"
24+ "golang.org/x/text/cases"
25+ "golang.org/x/text/language"
2226)
2327
2428const codeSampleExtensionName = "x-codeSamples"
@@ -121,6 +125,41 @@ func newAtlasCliCodeSamplesForOperation(op *openapi3.Operation) codeSample {
121125 }
122126}
123127
128+ func (f * CodeSampleFilter ) newGoSdkCodeSamplesForOperation (op * openapi3.Operation , opMethod string ) codeSample {
129+ version := strings .ReplaceAll (apiVersion (f .metadata .targetVersion ), "-" , "" ) + "001"
130+ 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 )
134+
135+ switch opMethod {
136+ case "GET" , "POST" , "PATCH" , "PUT" :
137+ sdkCall = " sdkResp, httpResp, err := " + sdkCall
138+ case "DELETE" :
139+ sdkCall = " httpResp, err := " + sdkCall
140+ }
141+
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 }"
155+
156+ return codeSample {
157+ Lang : "go" ,
158+ Label : "Go" ,
159+ Source : source ,
160+ }
161+ }
162+
124163func (f * CodeSampleFilter ) includeCodeSamplesForOperation (pathName , opMethod string , op * openapi3.Operation ) error {
125164 if op == nil || opMethod == "" || pathName == "" {
126165 return nil
@@ -130,10 +169,18 @@ func (f *CodeSampleFilter) includeCodeSamplesForOperation(pathName, opMethod str
130169 op .Extensions = map [string ]any {}
131170 }
132171
133- op . Extensions [ codeSampleExtensionName ] = []codeSample {
172+ codeSamples : = []codeSample {
134173 newAtlasCliCodeSamplesForOperation (op ),
135- f .newServiceAccountCurlCodeSamplesForOperation (pathName , opMethod ),
136- f .newDigestCurlCodeSamplesForOperation (pathName , opMethod ),
137174 }
175+
176+ if f .metadata .targetVersion .IsStable () {
177+ codeSamples = append (codeSamples , f .newGoSdkCodeSamplesForOperation (op , opMethod ))
178+ }
179+
180+ codeSamples = append (
181+ codeSamples ,
182+ f .newServiceAccountCurlCodeSamplesForOperation (pathName , opMethod ),
183+ f .newDigestCurlCodeSamplesForOperation (pathName , opMethod ))
184+ op .Extensions [codeSampleExtensionName ] = codeSamples
138185 return nil
139186}
0 commit comments