Skip to content

Commit c8be658

Browse files
authored
Set integer fields as pointers (#274)
Closes: #273 #245 oxidecomputer/terraform-provider-oxide#403
1 parent 39db29e commit c8be658

File tree

12 files changed

+399
-380
lines changed

12 files changed

+399
-380
lines changed

.changelog/v0.4.0.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[breaking]]
2-
title = ""
3-
description = ""
2+
title = "Integers as pointers"
3+
description = "All integers within the SDK's types are now `*int`. This is due to Go's handling of 0 as the empty value. This is specifically necessary when a field is an integer and also not required. [#274](https://github.com/oxidecomputer/oxide.go/pull/274)"
44

55
[[features]]
66
title = "Affinity and anti-affinity groups"

internal/generate/paths.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ func buildPathOrQueryParams(paramType string, params map[string]*openapi3.Parame
371371
pathParams = append(pathParams, fmt.Sprintf("%q: strconv.FormatBool(%s),", name, n))
372372
case "*bool":
373373
pathParams = append(pathParams, fmt.Sprintf("%q: strconv.FormatBool(*%s),", name, n))
374-
case "int":
375-
pathParams = append(pathParams, fmt.Sprintf("%q: strconv.Itoa(%s),", name, n))
374+
case "*int":
375+
pathParams = append(pathParams, fmt.Sprintf("%q: PointerIntToStr(%s),", name, n))
376376
case "*time.Time":
377377
pathParams = append(pathParams, fmt.Sprintf("%q: %s.Format(time.RFC3339),", name, n))
378378
default:

internal/generate/templates/listall_method.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
}{{end}}
55
var allPages {{.ResponseType}}
66
params.PageToken = ""
7-
params.Limit = 100
7+
params.Limit = NewPointer(100)
88
for {
99
page, err := c.{{.WrappedFunction}}(ctx, params)
1010
if err != nil {

internal/generate/test_utils/paths_output

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (c *Client) IpPoolList(ctx context.Context, params IpPoolListParams, ) (*Ip
2222
map[string]string{
2323
},
2424
map[string]string{
25-
"limit": strconv.Itoa(params.Limit),
25+
"limit": PointerIntToStr(params.Limit),
2626
"page_token": params.PageToken,
2727
"sort_by": string(params.SortBy),
2828
},
@@ -67,7 +67,7 @@ func (c *Client) IpPoolListAllPages(ctx context.Context, params IpPoolListParams
6767
}
6868
var allPages []IpPool
6969
params.PageToken = ""
70-
params.Limit = 100
70+
params.Limit = NewPointer(100)
7171
for {
7272
page, err := c.IpPoolList(ctx, params)
7373
if err != nil {

internal/generate/test_utils/paths_output_expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (c *Client) IpPoolList(ctx context.Context, params IpPoolListParams, ) (*Ip
2222
map[string]string{
2323
},
2424
map[string]string{
25-
"limit": strconv.Itoa(params.Limit),
25+
"limit": PointerIntToStr(params.Limit),
2626
"page_token": params.PageToken,
2727
"sort_by": string(params.SortBy),
2828
},
@@ -67,7 +67,7 @@ func (c *Client) IpPoolListAllPages(ctx context.Context, params IpPoolListParams
6767
}
6868
var allPages []IpPool
6969
params.PageToken = ""
70-
params.Limit = 100
70+
params.Limit = NewPointer(100)
7171
for {
7272
page, err := c.IpPoolList(ctx, params)
7373
if err != nil {

internal/generate/types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ func writeTypes(f *os.File, typeCollection []TypeTemplate, typeValidationCollect
354354
fmt.Fprint(f, " {\n")
355355
for _, ft := range tt.Fields {
356356
if ft.Description != "" {
357-
// TODO: Double check about the "//"
358357
fmt.Fprintf(f, "\t%s\n", splitDocString(ft.Description))
359358
}
360359
fmt.Fprintf(f, "\t%s %s %s\n", ft.Name, ft.Type, ft.SerializationInfo)
@@ -380,7 +379,7 @@ func writeTypes(f *os.File, typeCollection []TypeTemplate, typeValidationCollect
380379
fmt.Fprintf(f, "v.HasRequiredStr(string(p.%s), \"%s\")\n", s, s)
381380
}
382381
for _, i := range vm.RequiredNums {
383-
fmt.Fprintf(f, "v.HasRequiredNum(int(p.%s), \"%s\")\n", i, i)
382+
fmt.Fprintf(f, "v.HasRequiredNum(p.%s, \"%s\")\n", i, i)
384383
}
385384
fmt.Fprintln(f, "if !v.IsValid() {")
386385
// Unfortunately I have to craft the following line this way as I get

internal/generate/utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ func convertToValidGoType(property string, r *openapi3.SchemaRef) string {
139139
if r.Value.Type.Is("string") {
140140
schemaType = formatStringType(r.Value)
141141
} else if r.Value.Type.Is("integer") {
142-
schemaType = "int"
142+
// It is necessary to use pointers for integer types as we need
143+
// to differentiate between an empty value and a 0.
144+
schemaType = "*int"
143145
} else if r.Value.Type.Is("number") {
144146
schemaType = "float64"
145147
} else if r.Value.Type.Is("boolean") {

0 commit comments

Comments
 (0)