forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
82 lines (72 loc) · 1.81 KB
/
node.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
package diff
import (
"fmt"
"github.com/go-openapi/spec"
)
// Node is the position od a diff in a spec
type Node struct {
Field string `json:"name,omitempty"`
TypeName string `json:"type,omitempty"`
IsArray bool `json:"is_array,omitempty"`
ChildNode *Node `json:"child,omitempty"`
}
// String std string render
func (n *Node) String() string {
name := n.Field
if n.IsArray {
name = fmt.Sprintf("%s<array[%s]>", name, n.TypeName)
} else if len(n.TypeName) > 0 {
name = fmt.Sprintf("%s<%s>", name, n.TypeName)
}
if n.ChildNode != nil {
return fmt.Sprintf("%s.%s", name, n.ChildNode.String())
}
return name
}
// AddLeafNode Adds (recursive) a Child to the first non-nil child found
func (n *Node) AddLeafNode(toAdd *Node) *Node {
if n.ChildNode == nil {
n.ChildNode = toAdd
} else {
n.ChildNode.AddLeafNode(toAdd)
}
return n
}
// Copy deep copy of this node and children
func (n Node) Copy() *Node {
newChild := n.ChildNode
if newChild != nil {
newChild = newChild.Copy()
}
newNode := Node{
Field: n.Field,
TypeName: n.TypeName,
IsArray: n.IsArray,
ChildNode: newChild,
}
return &newNode
}
func getSchemaDiffNode(name string, schema interface{}) *Node {
node := Node{
Field: name,
}
if schema != nil {
switch s := schema.(type) {
case spec.Refable:
node.TypeName, node.IsArray = getSchemaType(s)
case *spec.Schema:
node.TypeName, node.IsArray = getSchemaType(s.SchemaProps)
case spec.SimpleSchema:
node.TypeName, node.IsArray = getSchemaType(s)
case *spec.SimpleSchema:
node.TypeName, node.IsArray = getSchemaType(s)
case *spec.SchemaProps:
node.TypeName, node.IsArray = getSchemaType(s)
case spec.SchemaProps:
node.TypeName, node.IsArray = getSchemaType(&s)
default:
node.TypeName = fmt.Sprintf("Unknown type %v", schema)
}
}
return &node
}