-
Notifications
You must be signed in to change notification settings - Fork 53
/
support.go
145 lines (117 loc) · 2.85 KB
/
support.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
package schema
import (
"fmt"
"strings"
)
type HasSchema interface {
Schema() Schema
}
type HasSchemaIO interface {
GetInputSchema(name string) Schema
GetOutputSchema(name string) Schema
}
//DEPRECATED
type ValidationBypass interface {
BypassValidation() bool
}
func New(schemaDef *Def) (Schema, error) {
if !enabled {
return emptySchema, nil
}
if !validationEnabled {
// validation disabled, so return non-validating schema
return &schemaSansValidation{def: schemaDef}, nil
}
factory := GetFactory(schemaDef.Type)
if factory == nil {
return nil, fmt.Errorf("support for schema type '%s' not installed", schemaDef.Type)
}
s, err := factory.New(schemaDef)
if err != nil {
return nil, err
}
return s, nil
}
func FindOrCreate(schemaRep interface{}) (Schema, error) {
switch t := schemaRep.(type) {
case HasSchema:
return t.Schema(), nil
case Def:
return New(&t)
case *Def:
return New(t)
case string:
if strings.HasPrefix(t, "schema://") {
id := t[9:]
s := Get(t[9:])
if s == nil {
sh := &schemaHolder{id: id}
toResolve = append(toResolve, sh)
s = sh
}
return s, nil
}
return nil, fmt.Errorf("invalid schema reference: %s", t)
case map[string]string:
def := &Def{}
if sType, ok := t["type"]; ok {
def.Type = sType
} else {
return nil, fmt.Errorf("invalid schema definition, type not specified: %+v", t)
}
if sValue, ok := t["value"]; ok {
def.Value = sValue
} else {
return nil, fmt.Errorf("invalid schema definition, value not specified: %+v", t)
}
return New(def)
case map[string]interface{}:
def := &Def{}
if sType, ok := t["type"]; ok {
def.Type, ok = sType.(string)
if !ok {
return nil, fmt.Errorf("invalid schema definition, type is not a string specified: %+v", sType)
}
} else {
return nil, fmt.Errorf("invalid schema definition, type not specified: %+v", t)
}
if sValue, ok := t["value"]; ok {
def.Value, ok = sValue.(string)
if !ok {
return nil, fmt.Errorf("invalid schema definition, value is not a string specified: %+v", sValue)
}
} else {
return nil, fmt.Errorf("invalid schema definition, value not specified: %+v", t)
}
return New(def)
case nil:
return nil, nil
default:
return nil, fmt.Errorf("invalid schema definition, %v", t)
}
}
var emptySchema = &emptySchemaImpl{}
type emptySchemaImpl struct {
}
func (*emptySchemaImpl) Type() string {
return ""
}
func (*emptySchemaImpl) Value() string {
return ""
}
func (*emptySchemaImpl) Validate(data interface{}) error {
return nil
}
// schemaSansValidation holds the schema information and ignores validation
type schemaSansValidation struct {
def *Def
}
func (s *schemaSansValidation) Type() string {
return s.def.Type
}
func (s *schemaSansValidation) Value() string {
return s.def.Value
}
func (*schemaSansValidation) Validate(data interface{}) error {
return nil
}