goTypeToSchemaType maps a Go reflect.Type to a JSON schema type string.
(t reflect.Type)
| 210 | // goTypeToSchemaType maps a Go reflect.Type to a JSON schema type |
| 211 | // string. |
| 212 | func goTypeToSchemaType(t reflect.Type) string { |
| 213 | // Dereference pointers. |
| 214 | for t.Kind() == reflect.Ptr { |
| 215 | t = t.Elem() |
| 216 | } |
| 217 | |
| 218 | // decimal.Decimal represents a precise numeric value and should |
| 219 | // map to the "number" schema type. |
| 220 | if t == reflect.TypeOf(decimal.Decimal{}) { |
| 221 | return "number" |
| 222 | } |
| 223 | |
| 224 | switch t.Kind() { |
| 225 | case reflect.String: |
| 226 | return "string" |
| 227 | case reflect.Bool: |
| 228 | return "boolean" |
| 229 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 230 | return "integer" |
| 231 | case reflect.Float32, reflect.Float64: |
| 232 | return "number" |
| 233 | case reflect.Slice: |
| 234 | return "array" |
| 235 | case reflect.Map: |
| 236 | return "object" |
| 237 | default: |
| 238 | return "string" |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // inferInputType decides the appropriate frontend input widget for |
| 243 | // a field based on its schema type and enum values. |