-
Notifications
You must be signed in to change notification settings - Fork 10
/
schema.go
71 lines (53 loc) · 1.16 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
// Copyright 2012-2014, Rolf Veen and contributors.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ogdl
// Check returns true if the Graph given as a parameter conforms to the
// schema represented by the receiver Graph.
func (g *Graph) Check(x *Graph) (bool, string) {
for i := 0; i < g.Len(); i++ {
ns := g.GetAt(i)
nx := x.GetAt(i)
b := ns.checkNode(nx)
if !b {
if ns != nil {
return false, "want " + ns.ThisString() + ", got " + nx.ThisString()
}
}
ok, mess := ns.Check(nx)
if !ok {
return false, mess
}
}
return true, ""
}
func (g *Graph) checkNode(x *Graph) bool {
if g == nil || x == nil {
return false
}
sx := x.ThisString()
sc := g.ThisString()
ty := x.ThisType()
var ok bool
// literal | literal* | literal? | literal+ | ** | ***
if sc[0] != '!' {
return sx == sc
}
// some type
switch sc {
case "!float":
_, ok = _float64f(x.This)
return ok
case "!int":
_, ok = _int64f(x.This)
return ok
case "!bool":
_, ok = _boolf(x.This)
return ok
case "!string":
return ty == "string"
case "!binary":
return ty == "[]byte"
}
return false
}