|
| 1 | +package codegen |
| 2 | + |
| 3 | +// SimpleTypeSpec defines the Go type for an OpenAPI type/format combination, |
| 4 | +// along with any import required to use it. |
| 5 | +type SimpleTypeSpec struct { |
| 6 | + Type string `yaml:"type" json:"type"` |
| 7 | + Import string `yaml:"import,omitempty" json:"import,omitempty"` |
| 8 | +} |
| 9 | + |
| 10 | +// FormatMapping defines the default Go type and format-specific overrides |
| 11 | +// for an OpenAPI type. |
| 12 | +type FormatMapping struct { |
| 13 | + Default SimpleTypeSpec `yaml:"default" json:"default"` |
| 14 | + Formats map[string]SimpleTypeSpec `yaml:"formats,omitempty" json:"formats,omitempty"` |
| 15 | +} |
| 16 | + |
| 17 | +// TypeMapping defines the mapping from OpenAPI types to Go types. |
| 18 | +type TypeMapping struct { |
| 19 | + Integer FormatMapping `yaml:"integer,omitempty" json:"integer,omitempty"` |
| 20 | + Number FormatMapping `yaml:"number,omitempty" json:"number,omitempty"` |
| 21 | + Boolean FormatMapping `yaml:"boolean,omitempty" json:"boolean,omitempty"` |
| 22 | + String FormatMapping `yaml:"string,omitempty" json:"string,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +// Merge returns a new TypeMapping with user overrides applied on top of base. |
| 26 | +func (base TypeMapping) Merge(user TypeMapping) TypeMapping { |
| 27 | + return TypeMapping{ |
| 28 | + Integer: base.Integer.merge(user.Integer), |
| 29 | + Number: base.Number.merge(user.Number), |
| 30 | + Boolean: base.Boolean.merge(user.Boolean), |
| 31 | + String: base.String.merge(user.String), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (base FormatMapping) merge(user FormatMapping) FormatMapping { |
| 36 | + result := FormatMapping{ |
| 37 | + Default: base.Default, |
| 38 | + Formats: make(map[string]SimpleTypeSpec), |
| 39 | + } |
| 40 | + |
| 41 | + // Copy base formats |
| 42 | + for k, v := range base.Formats { |
| 43 | + result.Formats[k] = v |
| 44 | + } |
| 45 | + |
| 46 | + // Override with user default if specified |
| 47 | + if user.Default.Type != "" { |
| 48 | + result.Default = user.Default |
| 49 | + } |
| 50 | + |
| 51 | + // Override/add user formats |
| 52 | + for k, v := range user.Formats { |
| 53 | + result.Formats[k] = v |
| 54 | + } |
| 55 | + |
| 56 | + return result |
| 57 | +} |
| 58 | + |
| 59 | +// Resolve returns the SimpleTypeSpec for a given format string. |
| 60 | +// If the format has a specific mapping, that is returned; otherwise the default is used. |
| 61 | +func (fm FormatMapping) Resolve(format string) SimpleTypeSpec { |
| 62 | + if format != "" { |
| 63 | + if spec, ok := fm.Formats[format]; ok { |
| 64 | + return spec |
| 65 | + } |
| 66 | + } |
| 67 | + return fm.Default |
| 68 | +} |
| 69 | + |
| 70 | +// DefaultTypeMapping provides the default OpenAPI type/format to Go type mappings. |
| 71 | +var DefaultTypeMapping = TypeMapping{ |
| 72 | + Integer: FormatMapping{ |
| 73 | + Default: SimpleTypeSpec{Type: "int"}, |
| 74 | + Formats: map[string]SimpleTypeSpec{ |
| 75 | + "int": {Type: "int"}, |
| 76 | + "int8": {Type: "int8"}, |
| 77 | + "int16": {Type: "int16"}, |
| 78 | + "int32": {Type: "int32"}, |
| 79 | + "int64": {Type: "int64"}, |
| 80 | + "uint": {Type: "uint"}, |
| 81 | + "uint8": {Type: "uint8"}, |
| 82 | + "uint16": {Type: "uint16"}, |
| 83 | + "uint32": {Type: "uint32"}, |
| 84 | + "uint64": {Type: "uint64"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + Number: FormatMapping{ |
| 88 | + Default: SimpleTypeSpec{Type: "float32"}, |
| 89 | + Formats: map[string]SimpleTypeSpec{ |
| 90 | + "float": {Type: "float32"}, |
| 91 | + "double": {Type: "float64"}, |
| 92 | + }, |
| 93 | + }, |
| 94 | + Boolean: FormatMapping{ |
| 95 | + Default: SimpleTypeSpec{Type: "bool"}, |
| 96 | + }, |
| 97 | + String: FormatMapping{ |
| 98 | + Default: SimpleTypeSpec{Type: "string"}, |
| 99 | + Formats: map[string]SimpleTypeSpec{ |
| 100 | + "byte": {Type: "[]byte"}, |
| 101 | + "email": {Type: "openapi_types.Email"}, |
| 102 | + "date": {Type: "openapi_types.Date"}, |
| 103 | + "date-time": {Type: "time.Time", Import: "time"}, |
| 104 | + "json": {Type: "json.RawMessage", Import: "encoding/json"}, |
| 105 | + "uuid": {Type: "openapi_types.UUID"}, |
| 106 | + "binary": {Type: "openapi_types.File"}, |
| 107 | + }, |
| 108 | + }, |
| 109 | +} |
0 commit comments