Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .changelog/v0.4.0.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[breaking]]
title = ""
description = ""
title = "Integers as pointers"
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)"

[[features]]
title = "Affinity and anti-affinity groups"
Expand Down
4 changes: 2 additions & 2 deletions internal/generate/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ func buildPathOrQueryParams(paramType string, params map[string]*openapi3.Parame
pathParams = append(pathParams, fmt.Sprintf("%q: strconv.FormatBool(%s),", name, n))
case "*bool":
pathParams = append(pathParams, fmt.Sprintf("%q: strconv.FormatBool(*%s),", name, n))
case "int":
pathParams = append(pathParams, fmt.Sprintf("%q: strconv.Itoa(%s),", name, n))
case "*int":
pathParams = append(pathParams, fmt.Sprintf("%q: PointerIntToStr(%s),", name, n))
case "*time.Time":
pathParams = append(pathParams, fmt.Sprintf("%q: %s.Format(time.RFC3339),", name, n))
default:
Expand Down
2 changes: 1 addition & 1 deletion internal/generate/templates/listall_method.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
}{{end}}
var allPages {{.ResponseType}}
params.PageToken = ""
params.Limit = 100
params.Limit = NewPointer(100)
for {
page, err := c.{{.WrappedFunction}}(ctx, params)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/generate/test_utils/paths_output
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (c *Client) IpPoolList(ctx context.Context, params IpPoolListParams, ) (*Ip
map[string]string{
},
map[string]string{
"limit": strconv.Itoa(params.Limit),
"limit": PointerIntToStr(params.Limit),
"page_token": params.PageToken,
"sort_by": string(params.SortBy),
},
Expand Down Expand Up @@ -67,7 +67,7 @@ func (c *Client) IpPoolListAllPages(ctx context.Context, params IpPoolListParams
}
var allPages []IpPool
params.PageToken = ""
params.Limit = 100
params.Limit = NewPointer(100)
for {
page, err := c.IpPoolList(ctx, params)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/generate/test_utils/paths_output_expected
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (c *Client) IpPoolList(ctx context.Context, params IpPoolListParams, ) (*Ip
map[string]string{
},
map[string]string{
"limit": strconv.Itoa(params.Limit),
"limit": PointerIntToStr(params.Limit),
"page_token": params.PageToken,
"sort_by": string(params.SortBy),
},
Expand Down Expand Up @@ -67,7 +67,7 @@ func (c *Client) IpPoolListAllPages(ctx context.Context, params IpPoolListParams
}
var allPages []IpPool
params.PageToken = ""
params.Limit = 100
params.Limit = NewPointer(100)
for {
page, err := c.IpPoolList(ctx, params)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/generate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ func writeTypes(f *os.File, typeCollection []TypeTemplate, typeValidationCollect
fmt.Fprint(f, " {\n")
for _, ft := range tt.Fields {
if ft.Description != "" {
// TODO: Double check about the "//"
fmt.Fprintf(f, "\t%s\n", splitDocString(ft.Description))
}
fmt.Fprintf(f, "\t%s %s %s\n", ft.Name, ft.Type, ft.SerializationInfo)
Expand All @@ -380,7 +379,7 @@ func writeTypes(f *os.File, typeCollection []TypeTemplate, typeValidationCollect
fmt.Fprintf(f, "v.HasRequiredStr(string(p.%s), \"%s\")\n", s, s)
}
for _, i := range vm.RequiredNums {
fmt.Fprintf(f, "v.HasRequiredNum(int(p.%s), \"%s\")\n", i, i)
fmt.Fprintf(f, "v.HasRequiredNum(p.%s, \"%s\")\n", i, i)
}
fmt.Fprintln(f, "if !v.IsValid() {")
// Unfortunately I have to craft the following line this way as I get
Expand Down
4 changes: 3 additions & 1 deletion internal/generate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ func convertToValidGoType(property string, r *openapi3.SchemaRef) string {
if r.Value.Type.Is("string") {
schemaType = formatStringType(r.Value)
} else if r.Value.Type.Is("integer") {
schemaType = "int"
// It is necessary to use pointers for integer types as we need
// to differentiate between an empty value and a 0.
schemaType = "*int"
} else if r.Value.Type.Is("number") {
schemaType = "float64"
} else if r.Value.Type.Is("boolean") {
Expand Down
Loading