forked from fabric8-services/fabric8-wit
/
field_definition.go
152 lines (136 loc) · 3.57 KB
/
field_definition.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
package models
import (
"bytes"
"encoding/json"
"fmt"
)
// constants for describing possible field types
const (
String Kind = 1
Integer Kind = 2
Float Kind = 3
Instant Kind = 4
Duration Kind = 5
Url Kind = 6
WorkitemReference Kind = 7
User Kind = 8
Enum Kind = 9
List Kind = 10
)
// Kind is the kind of field type
type Kind byte
/*
FieldType describes the possible values of a FieldDefinition
*/
type FieldType interface {
GetKind() Kind
/*
ConvertToModel converts a field value for use in the REST API
*/
ConvertToModel(value interface{}) (interface{}, error)
/*
ConvertToModel converts a field value for storage in the db
*/
ConvertFromModel(value interface{}) (interface{}, error)
}
/*
FieldDefintion describes type & other restrictions of a field
*/
type FieldDefinition struct {
Required bool
Type FieldType
}
/*
Convert a field value for storage as json. As the system matures, add more checks (for example whether a user is in the system, etc.)
*/
func (field FieldDefinition) ConvertToModel(name string, value interface{}) (interface{}, error) {
if field.Required && value == nil {
return nil, fmt.Errorf("Value %s is required", name)
}
return field.Type.ConvertToModel(value)
}
/*
Convert from json storage to API form.
*/
func (field FieldDefinition) ConvertFromModel(name string, value interface{}) (interface{}, error) {
if field.Required && value == nil {
return nil, fmt.Errorf("Value %s is required", name)
}
return field.Type.ConvertFromModel(value)
}
type rawFieldDef struct {
Type rawFieldType
Required bool
}
type rawEnumType struct {
BaseType SimpleType
Values []interface{}
}
type rawFieldType struct {
Kind Kind
Extra *json.RawMessage
}
// UnmarshalJSON implements encoding/json.Unmarshaler
func (self *FieldDefinition) UnmarshalJSON(bytes []byte) error {
temp := rawFieldDef{}
err := json.Unmarshal(bytes, &temp)
if err != nil {
return err
}
switch temp.Type.Kind {
case List:
var baseType SimpleType
err = json.Unmarshal(*temp.Type.Extra, &baseType)
if err != nil {
return err
}
theType := ListType{SimpleType: SimpleType{Kind: temp.Type.Kind}, ComponentType: baseType}
*self = FieldDefinition{Type: theType, Required: temp.Required}
case Enum:
var extraInfo rawEnumType
err = json.Unmarshal(*temp.Type.Extra, &extraInfo)
if err != nil {
return err
}
theType := EnumType{SimpleType: SimpleType{Kind: temp.Type.Kind}, BaseType: extraInfo.BaseType, Values: extraInfo.Values}
*self = FieldDefinition{Type: theType, Required: temp.Required}
default:
*self = FieldDefinition{Type: SimpleType{Kind: temp.Type.Kind}, Required: temp.Required}
}
return nil
}
// MarshalJSON implements encoding/json.Marshaler
func (self FieldDefinition) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("{ \"type\": {")
buf.WriteString(fmt.Sprintf("\"kind\": %d", self.Type.GetKind()))
switch complexType := self.Type.(type) {
case ListType:
buf.WriteString(", \"extra\": ")
v, err := json.Marshal(complexType.ComponentType)
if err != nil {
return nil, err
}
buf.Write(v)
case EnumType:
buf.WriteString(", \"extra\": ")
r := rawEnumType{
BaseType: complexType.BaseType,
Values: complexType.Values,
}
v, err := json.Marshal(r)
if err != nil {
return nil, err
}
buf.Write(v)
}
buf.WriteString("}")
buf.WriteString(", \"required\": ")
if self.Required {
buf.WriteString("true")
} else {
buf.WriteString("false")
}
buf.WriteString(" }")
return buf.Bytes(), nil
}