Skip to content

Commit 976d548

Browse files
CLOUDP-299213: Use int for HTTPCode instead of string (#3618)
1 parent 8cc67a7 commit 976d548

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

internal/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type WatcherGetProperties struct {
104104
}
105105

106106
type WatcherExpectProperties struct {
107-
HTTPCode string
107+
HTTPCode int
108108
Match *WatcherMatchProperties
109109
}
110110

tools/api-generator/.snapshots/04-spec-with-overrides.yaml.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ var Commands = GroupedAndSortedCommands{
104104
},
105105
},
106106
Expect: &WatcherExpectProperties{
107-
HTTPCode: ``,
107+
HTTPCode: 200,
108108
Match: &WatcherMatchProperties{
109109
Path: `$.stateName`,
110110
Values: []string{

tools/api-generator/commands.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ var Commands = GroupedAndSortedCommands{
8383
`orgId`: `123`,
8484
},
8585
},
86-
Expect: {{if not .Watcher.Expect }} nil,{{else}}&WatcherExpectProperties{
87-
HTTPCode: `{{ .Watcher.Expect.HTTPCode }}`,
86+
Expect: {{if not .Watcher.Expect }} nil,{{else}}&WatcherExpectProperties{ {{if ne .Watcher.Expect.HTTPCode 0}}
87+
HTTPCode: {{ .Watcher.Expect.HTTPCode }},{{- end }}
8888
Match: {{if not .Watcher.Expect.Match }} nil,{{else}}&WatcherMatchProperties{
8989
Path: `{{ .Watcher.Expect.Match.Path }}`,
9090
Values: []string{

tools/api-generator/watcher.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"errors"
19+
"fmt"
1920
"strings"
2021

2122
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/api"
@@ -105,7 +106,15 @@ func newWatcherGetProperties(ext map[string]any) (*api.WatcherGetProperties, err
105106
}
106107

107108
func newWatcherExpectProperties(ext map[string]any) (*api.WatcherExpectProperties, error) {
108-
httpCode, _ := ext["http-code"].(string)
109+
httpCode := 0
110+
if httpCodeValue, ok := ext["http-code"]; ok {
111+
// ext["http-code"] is passed from the yaml converter
112+
// the converter is entirely free to decide which integer type it returns, could be any numeric type
113+
var err error
114+
if httpCode, err = toInt(httpCodeValue); err != nil {
115+
return nil, err
116+
}
117+
}
109118

110119
var match *api.WatcherMatchProperties
111120
matchExt := extractObject(ext, "match")
@@ -157,9 +166,41 @@ func extractObject(ext map[string]any, name string) map[string]any {
157166
return nil
158167
}
159168

160-
/*
161-
func validateWatcherProperties(spec *openapi3.T, properties *api.WatcherProperties) error {
162-
// TODO
163-
return nil
169+
func toInt(value any) (int, error) {
170+
switch v := value.(type) {
171+
case int:
172+
return v, nil
173+
case int8:
174+
return int(v), nil
175+
case int16:
176+
return int(v), nil
177+
case int32:
178+
return int(v), nil
179+
case int64:
180+
return int(v), nil
181+
case uint:
182+
//nolint:gosec
183+
return int(v), nil
184+
case uint8:
185+
return int(v), nil
186+
case uint16:
187+
return int(v), nil
188+
case uint32:
189+
return int(v), nil
190+
case uint64:
191+
//nolint:gosec
192+
return int(v), nil
193+
case float32:
194+
if float32(int(v)) != v {
195+
return 0, fmt.Errorf("value %v has decimal places", v)
196+
}
197+
return int(v), nil
198+
case float64:
199+
if float64(int(v)) != v {
200+
return 0, fmt.Errorf("value %v has decimal places", v)
201+
}
202+
return int(v), nil
203+
default:
204+
return 0, fmt.Errorf("value %v of type %T cannot be converted to int", value, value)
205+
}
164206
}
165-
*/

tools/api-generator/watcher_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestNewWatcherExpectProperties(t *testing.T) {
122122
}{
123123
{
124124
input: map[string]any{
125-
"http-code": "200",
125+
"http-code": 200,
126126
"match": map[string]any{
127127
"values": "IDLE",
128128
},
@@ -132,7 +132,7 @@ func TestNewWatcherExpectProperties(t *testing.T) {
132132
},
133133
{
134134
input: map[string]any{
135-
"http-code": "200",
135+
"http-code": 200,
136136
"match": map[string]any{
137137
"path": "$.stateName",
138138
},
@@ -142,14 +142,14 @@ func TestNewWatcherExpectProperties(t *testing.T) {
142142
},
143143
{
144144
input: map[string]any{
145-
"http-code": "200",
145+
"http-code": 200,
146146
"match": map[string]any{
147147
"path": "$.stateName",
148148
"values": "IDLE",
149149
},
150150
},
151151
expectedOutput: &api.WatcherExpectProperties{
152-
HTTPCode: "200",
152+
HTTPCode: 200,
153153
Match: &api.WatcherMatchProperties{
154154
Path: "$.stateName",
155155
Values: []string{"IDLE"},
@@ -159,14 +159,14 @@ func TestNewWatcherExpectProperties(t *testing.T) {
159159
},
160160
{
161161
input: map[string]any{
162-
"http-code": "200",
162+
"http-code": 200,
163163
"match": map[string]any{
164164
"path": "$.stateName",
165165
"values": "IDLE,IDLE2",
166166
},
167167
},
168168
expectedOutput: &api.WatcherExpectProperties{
169-
HTTPCode: "200",
169+
HTTPCode: 200,
170170
Match: &api.WatcherMatchProperties{
171171
Path: "$.stateName",
172172
Values: []string{"IDLE", "IDLE2"},

0 commit comments

Comments
 (0)