-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
63 lines (50 loc) · 1.52 KB
/
checker.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
package polyschema
import "encoding/json"
type TypesIdentity int8
const (
// TypesEqual means that the type A is identical to the type B
TypesEqual TypesIdentity = 0
// TypesNotEqual means that the type B is neither subtype nor equal type to the A
TypesNotEqual TypesIdentity = -1
// TypesSubtype means that the type B is subtype of the A
TypesSubtype TypesIdentity = 1
)
// Subtype checks if child is subtype or equal type to parent
func Subtype(parent, child JsonSchema) TypesIdentity {
if eq := checkTypeIdentity(parent, child); eq == TypesNotEqual {
return TypesNotEqual
}
switch *parent.Type {
case Object:
return checkObject(parent, child)
case String:
return checkString(parent, child)
case Number:
return checkNumber(parent, child)
case Integer:
return checkNumber(parent, child)
case Array:
return checkArray(parent, child)
}
return TypesNotEqual
}
// SubtypeRaw unmarshalls raw schemas and checks if child is subtype or equal type to parent
func SubtypeRaw(parentRaw, childRaw string) (TypesIdentity, error) {
var parent, child JsonSchema
if err := json.Unmarshal([]byte(parentRaw), &parent); err != nil {
return TypesNotEqual, err
}
if err := json.Unmarshal([]byte(childRaw), &child); err != nil {
return TypesNotEqual, err
}
return Subtype(parent, child), nil
}
func checkTypeIdentity(schema1, schema2 JsonSchema) TypesIdentity {
if schema1.Type == nil || schema2.Type == nil {
return TypesNotEqual
}
if *schema1.Type != *schema2.Type {
return TypesNotEqual
}
return TypesEqual
}