/
types.go
206 lines (178 loc) · 5.12 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"go/types"
"github.com/pkg/errors"
)
var (
elmBool = &ElmBasicType{name: "Bool", codec: "bool"}
elmFloat = &ElmBasicType{name: "Float", codec: "float"}
elmInt = &ElmBasicType{name: "Int", codec: "int"}
elmString = &ElmBasicType{name: "String", codec: "string"}
)
// ElmType represents a type in Elm.
type ElmType interface {
Name() string
Decoder(prefix string) string
Encoder(prefix string) string
Equal(other ElmType) bool
Nullable() bool
}
func elmTypeName(t ElmType) string {
if t == nil {
return "<undefined>"
}
return t.Name()
}
// ElmBasicType represents primitive types in Elm.
type ElmBasicType struct {
name string
codec string
}
// Name returns the name of the Elm type.
func (t *ElmBasicType) Name() string {
return t.name
}
// Decoder returns the name of the Elm JSON encoder/decoder for this type.
func (t *ElmBasicType) Decoder(prefix string) string {
return prefix + "." + t.codec
}
// Encoder returns the name of the Elm JSON encoder/decoder for this type.
func (t *ElmBasicType) Encoder(prefix string) string {
return prefix + "." + t.codec
}
// Equal tests for equality with another ElmType.
func (t *ElmBasicType) Equal(other ElmType) bool {
if o, ok := other.(*ElmBasicType); ok {
return t.name == o.name &&
t.codec == o.codec
}
return false
}
// Nullable indicates whether this type can be nil.
func (t *ElmBasicType) Nullable() bool {
return false
}
// ElmList represents a list of another type.
type ElmList struct {
elem ElmType
}
// Name returns the name of the Elm type.
func (t *ElmList) Name() string {
return "List " + t.elem.Name()
}
// Decoder returns the name of the Elm JSON encoder/decoder for this type.
func (t *ElmList) Decoder(prefix string) string {
return "(" + prefix + ".list " + t.elem.Decoder(prefix) + ")"
}
// Encoder returns the name of the Elm JSON encoder/decoder for this type.
func (t *ElmList) Encoder(prefix string) string {
return "(" + prefix + ".list " + t.elem.Encoder(prefix) + ")"
}
// Equal tests for equality with another ElmType.
func (t *ElmList) Equal(other ElmType) bool {
if o, ok := other.(*ElmList); ok {
return t.elem.Equal(o.elem)
}
return false
}
// Nullable indicates whether this type can be nil.
func (t *ElmList) Nullable() bool {
return true
}
// ElmPointer represents a pointer to an instance of another type.
type ElmPointer struct {
elem ElmType
}
// Name returns the name of the Elm type.
func (t *ElmPointer) Name() string {
return t.elem.Name()
}
// Decoder returns the name of the Elm JSON encoder/decoder for this type.
func (t *ElmPointer) Decoder(prefix string) string {
return t.elem.Decoder(prefix)
}
// Encoder returns the name of the Elm JSON encoder/decoder for this type.
func (t *ElmPointer) Encoder(prefix string) string {
return t.elem.Encoder(prefix)
}
// Equal tests for equality with another ElmType.
func (t *ElmPointer) Equal(other ElmType) bool {
if o, ok := other.(*ElmPointer); ok {
return t.elem.Equal(o.elem)
}
return false
}
// Nullable indicates whether this type can be nil.
func (t *ElmPointer) Nullable() bool {
return true
}
// ElmTypeResolver maintains a cache of Go to Elm type conversions.
type ElmTypeResolver struct {
resolved map[string]*ElmRecord
ordered []*ElmRecord
renames TypeNamePairs
}
// NewResolver creates an empty resolver.
func NewResolver(renames TypeNamePairs) *ElmTypeResolver {
return &ElmTypeResolver{
resolved: make(map[string]*ElmRecord),
renames: renames,
}
}
// Convert translates a Go type into an Elm type and JSON decoder pair.
func (r *ElmTypeResolver) Convert(goType types.Type) (ElmType, error) {
switch t := goType.(type) {
case *types.Basic:
switch t.Kind() {
case types.Bool:
return elmBool, nil
case types.Float32, types.Float64:
return elmFloat, nil
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64:
return elmInt, nil
case types.String:
return elmString, nil
}
case *types.Pointer:
elemType, err := r.Convert(t.Elem())
if err != nil {
return nil, err
}
return &ElmPointer{elem: elemType}, nil
case *types.Slice:
elemType, err := r.Convert(t.Elem())
if err != nil {
return nil, err
}
return &ElmList{elem: elemType}, nil
case *types.Named:
goName := t.Obj().Name()
switch u := t.Underlying().(type) {
case *types.Struct:
return r.resolveRecord(goName, u)
}
}
return nil, errors.Errorf("don't know how to handle Go type %s (%T)", goType, goType)
}
// CachedRecords returns slice of resolved Elm records.
func (r *ElmTypeResolver) CachedRecords() []*ElmRecord {
return r.ordered
}
// resolveRecord converts the struct to an Elm record, or returns the cached version.
func (r *ElmTypeResolver) resolveRecord(goName string, stype *types.Struct) (*ElmRecord, error) {
if record := r.resolved[goName]; record != nil {
return record, nil
}
record, err := recordFromStruct(r, stype, goName)
if err != nil {
return nil, err
}
logger.Debug().
Str("name", goName).
Str("type", elmTypeName(record)).
Msg("Caching resolved type")
r.resolved[goName] = record
r.ordered = append(r.ordered, record)
return record, nil
}