/
types.go
82 lines (74 loc) · 2.29 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package go2oapi
import (
"go/ast"
"go/types"
)
// Encodes the type of a field.
type DataType string
const (
Object DataType = "object"
Number DataType = "number"
Integer DataType = "integer"
String DataType = "string"
Array DataType = "array"
Null DataType = "null"
Boolean DataType = "boolean"
)
// FunctionDetails describes a Go function.
type FunctionDetails struct {
// Name of the function.
Name string `json:"name"`
// Description of the function.
Description string `json:"description"`
// Parameters of the function.
Parameters *Definition `json:"parameters"`
}
// Definition holds the name and type of a function parameter.
type Definition struct {
// Type specifies the data type of the schema.
Type DataType `json:"type,omitempty"`
// Description is the description of the schema.
Description string `json:"description,omitempty"`
// Enum is used to restrict a value to a fixed set of values. It must be an array with at least
// one element, where each element is unique. You will probably only use this with strings.
Enum []string `json:"enum,omitempty"`
// Properties describes the properties of an object, if the schema type is Object.
Properties map[string]*Definition `json:"properties,omitempty"`
// Required specifies which properties are required, if the schema type is Object.
Required []string `json:"required,omitempty"`
// Items specifies which data type an array contains, if the schema type is Array.
Items *Definition `json:"items,omitempty"`
}
func exprToType(info *types.Info, expr ast.Expr) DataType {
typ := info.TypeOf(expr)
if typ == nil {
return Null // or some error handling
}
switch typ := typ.Underlying().(type) {
case *types.Basic:
return basicTypeToDataType(typ)
case *types.Array, *types.Slice:
return Array
case *types.Struct:
return Object
case *types.Pointer:
return exprToType(info, &ast.Ident{Name: typ.Elem().String()})
default:
return Null
}
}
func basicTypeToDataType(basic *types.Basic) DataType {
switch basic.Kind() {
case types.Bool:
return Boolean
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
return Integer
case types.Float32, types.Float64:
return Number
case types.String:
return String
default:
return Null
}
}