Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion VERSION_OMICRON
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e44ad55
4a0cb6b
20 changes: 13 additions & 7 deletions internal/generate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"fmt"
"os"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -393,7 +394,7 @@ func populateTypeTemplates(name string, s *openapi3.Schema, enumFieldName string
typeTpl := TypeTemplate{}

// TODO: remove workaround once no more type objects are empty
if sliceContains(emptyTypes(), name) {
if slices.Contains(emptyTypes(), name) {
bgpOT := getObjectType(s)
if bgpOT != "" {
panic("[ERROR] " + name + " is no longer an empty type. Remove workaround in exceptions.go")
Expand Down Expand Up @@ -474,6 +475,7 @@ func createTypeObject(schema *openapi3.Schema, name, typeName, description strin
}

schemas := schema.Properties
required := schema.Required
fields := []TypeFields{}
keys := sortedKeys(schemas)
for _, k := range keys {
Expand Down Expand Up @@ -511,8 +513,12 @@ func createTypeObject(schema *openapi3.Schema, name, typeName, description strin
}
}

// If a type is nullable we'll want a pointer
if sliceContains(nullable(), typeName) {
// Omicron includes fields that are both required and nullable:
// they can be set to a null value, but they must not be
// omitted. The sdk should present these fields to the user as
// optional, and serialize them to `null` if not provided.
isRequiredNullable := v.Value.Nullable && slices.Contains(required, k)
if (slices.Contains(nullable(), typeName) || isRequiredNullable) && !strings.HasPrefix(typeName, "*") {
Comment thread
sudomateo marked this conversation as resolved.
Outdated
typeName = fmt.Sprintf("*%s", typeName)
}

Expand All @@ -528,9 +534,9 @@ func createTypeObject(schema *openapi3.Schema, name, typeName, description strin
// TODO: Set omitzero on all types.
// https://github.com/oxidecomputer/oxide.go/issues/290
serInfo := fmt.Sprintf("`json:\"%s,omitempty\" yaml:\"%s,omitempty\"`", k, k)
if isNullableArray(v) {
if isNullableArray(v) || isRequiredNullable {
serInfo = fmt.Sprintf("`json:\"%s\" yaml:\"%s\"`", k, k)
} else if sliceContains(omitzeroTypes(), typeName) {
} else if slices.Contains(omitzeroTypes(), typeName) {
serInfo = fmt.Sprintf("`json:\"%s,omitzero\" yaml:\"%s,omitzero\"`", k, k)
}

Expand Down Expand Up @@ -658,7 +664,7 @@ func createOneOf(s *openapi3.Schema, name, typeName string) ([]TypeTemplate, []E
for _, v := range properties {
parts := strings.Split(v, "=")
key := parts[0]
if !sliceContains(typeKeys, key) {
if !slices.Contains(typeKeys, key) {
typeKeys = append(typeKeys, key)
}
}
Expand Down Expand Up @@ -712,7 +718,7 @@ func createOneOf(s *openapi3.Schema, name, typeName string) ([]TypeTemplate, []E
}

// We set the type of a field as "any" if every element of the oneOf property isn't the same
if sliceContains(genericTypes, prop) {
if slices.Contains(genericTypes, prop) {
field.Type = "any"
}

Expand Down
16 changes: 1 addition & 15 deletions internal/generate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,7 @@ func verifyNotAGoType(str string) string {
func isNumericType(str string) bool {
numTypes := []string{"int", "int8", "int16", "int32", "int64", "uint", "uint8",
"uint16", "uint32", "uint64", "uintptr", "float32", "float64"}
for _, v := range numTypes {
if str == v {
return true
}
}
return false
}

func sliceContains[T comparable](s []T, str T) bool {
for _, a := range s {
if a == str {
return true
}
}
return false
return slices.Contains(numTypes, str)
}

func allItemsAreSame[T comparable](a []T) bool {
Expand Down
104 changes: 93 additions & 11 deletions oxide/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.