forked from alecthomas/hcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
111 lines (96 loc) · 2.9 KB
/
schema.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
package hcl
import (
"fmt"
"reflect"
)
// Schema reflects a schema from a Go value.
//
// A schema is itself HCL.
func Schema(v interface{}, options ...MarshalOption) (*AST, error) {
options = append(options, asSchema(true))
ast, err := marshalToAST(v, newMarshalState(options...))
if err != nil {
return nil, err
}
return ast, nil
}
// MustSchema constructs a schema from a Go type, or panics.
func MustSchema(v interface{}, options ...MarshalOption) *AST {
ast, err := Schema(v, options...)
if err != nil {
panic(err)
}
return ast
}
// BlockSchema reflects a block schema for a Go struct.
func BlockSchema(name string, v interface{}, options ...MarshalOption) (*AST, error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Struct {
return nil, fmt.Errorf("expected a pointer to a struct not %T", v)
}
options = append(options, asSchema(true))
block, err := valueToBlock(rv.Elem(), tag{name: name, block: true}, newMarshalState(options...))
if err != nil {
return nil, err
}
return &AST{
Entries: []*Entry{{Block: block}},
Schema: true,
}, nil
}
// MustBlockSchema reflects a block schema from a Go struct, panicking if an error occurs.
func MustBlockSchema(name string, v interface{}, options ...MarshalOption) *AST {
ast, err := BlockSchema(name, v, options...)
if err != nil {
panic(err)
}
return ast
}
var (
strType = "string"
numType = "number"
boolType = "boolean"
)
func attrSchema(t reflect.Type) (*Value, error) {
if t == durationType || t == timeType || typeImplements(t, textMarshalerInterface) || typeImplements(t, jsonMarshalerInterface) {
return &Value{Type: &strType}, nil
}
switch t.Kind() {
case reflect.String:
return &Value{Type: &strType}, nil
case reflect.Slice:
el, err := attrSchema(t.Elem())
if err != nil {
return nil, err
}
return &Value{List: []*Value{el}, HaveList: true}, nil
case reflect.Map:
el, err := attrSchema(t.Elem())
if err != nil {
return nil, err
}
return &Value{Map: []*MapEntry{{Key: &Value{Type: &strType}, Value: el}}, HaveMap: true}, nil
case reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return &Value{Type: &numType}, nil
case reflect.Bool:
return &Value{Type: &boolType}, nil
case reflect.Struct:
panic("struct " + t.String() + " used as attribute, is it missing a \"block\" tag?")
case reflect.Ptr:
return attrSchema(t.Elem())
default:
panic(fmt.Sprintf("unsupported attribute type %s during schema reflection", t))
}
}
func sliceToBlockSchema(t reflect.Type, tag tag, opt *marshalState) (*Block, error) {
block := &Block{
Name: tag.name,
Comments: tag.comments(opt),
Repeated: true,
}
var err error
block.Body, block.Labels, err = structToEntries(reflect.New(t.Elem()).Elem(), opt.withSchema(true))
return block, err
}