Skip to content

Commit 2e2506f

Browse files
feat: autogenerate go SDK example in the codeSample
1 parent 2678d1c commit 2e2506f

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

tools/cli/internal/openapi/filter/code_sample.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
package filter
1616

1717
import (
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

2428
const 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+
124163
func (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
}

tools/cli/internal/openapi/filter/code_sample_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func TestCodeSampleFilter(t *testing.T) {
5050
},
5151
},
5252
})),
53+
Tags: []string{"TestTag"},
5354
Extensions: map[string]any{
5455
"x-sunset": "9999-12-31",
5556
},
@@ -73,6 +74,7 @@ func TestCodeSampleFilter(t *testing.T) {
7374
},
7475
},
7576
})),
77+
Tags: []string{"TestTag"},
7678
Extensions: map[string]any{
7779
"x-sunset": "9999-12-31",
7880
"x-codeSamples": []codeSample{
@@ -81,6 +83,26 @@ func TestCodeSampleFilter(t *testing.T) {
8183
Label: "Atlas CLI",
8284
Source: "atlas api testOperationID --help",
8385
},
86+
{
87+
Lang: "go",
88+
Label: "Go",
89+
Source: "import (\n" +
90+
" \"os\"\n \"context\"\n" +
91+
" sdk \"go.mongodb.org/atlas-sdk/v20250101001/admin\"\n)\n\n" +
92+
"func main() {\n" +
93+
" ctx := context.Background()\n" +
94+
" apiKey := os.Getenv(\"MONGODB_ATLAS_PUBLIC_KEY\")\n" +
95+
" apiSecret := os.Getenv(\"MONGODB_ATLAS_PRIVATE_KEY\")\n" +
96+
" url := os.Getenv(\"MONGODB_ATLAS_BASE_URL\")\n\n" +
97+
" client, err := sdk.NewClient(\n" +
98+
" sdk.UseDigestAuth(apiKey, apiSecret),\n" +
99+
" sdk.UseBaseURL(url),\n" +
100+
" sdk.UseDebug(true))\n\n" +
101+
" params = &sdk.TestOperationIDApiParams{}\n" +
102+
" sdkResp, httpResp, err := sdk.TestTagApi\n" +
103+
" .TestOperationIDWithParams(ctx, params)\n" +
104+
" .Execute()" + "\n}",
105+
},
84106
{
85107
Lang: "cURL",
86108
Label: "curl (Service Accounts)",

0 commit comments

Comments
 (0)