-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.go
121 lines (108 loc) · 3.51 KB
/
example.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
package raml
import (
"errors"
"fmt"
orderedmap "github.com/wk8/go-ordered-map/v2"
"gopkg.in/yaml.v3"
"github.com/acronis/go-stacktrace"
)
const (
ExampleValue = "value"
)
var ErrValueKeyNotFound = errors.New("value key not found")
func (ex *Example) decode(node *yaml.Node, valueNode *yaml.Node, location string) error {
switch node.Value {
case FacetStrict:
if err := valueNode.Decode(&ex.Strict); err != nil {
return StacktraceNewWrapped("decode strict", err, location, WithNodePosition(valueNode))
}
case FacetDisplayName:
if err := valueNode.Decode(&ex.DisplayName); err != nil {
return StacktraceNewWrapped("decode displayName", err, location, WithNodePosition(valueNode))
}
case FacetDescription:
if err := valueNode.Decode(&ex.Description); err != nil {
return StacktraceNewWrapped("decode description", err, location, WithNodePosition(valueNode))
}
default:
if IsCustomDomainExtensionNode(node.Value) {
deName, de, err := ex.raml.unmarshalCustomDomainExtension(location, node, valueNode)
if err != nil {
return StacktraceNewWrapped("unmarshal custom domain extension", err, location, WithNodePosition(valueNode))
}
ex.CustomDomainProperties.Set(deName, de)
}
}
return nil
}
func (ex *Example) fill(location string, value *yaml.Node) error {
var valueKey *yaml.Node
// First lookup for the "value" key.
for i := 0; i != len(value.Content); i += 2 {
node := value.Content[i]
valueNode := value.Content[i+1]
if node.Value == ExampleValue {
valueKey = valueNode
break
}
}
if valueKey == nil {
return ErrValueKeyNotFound
}
// If "value" key is found, then the example is considered as a map with additional properties
for i := 0; i != len(value.Content); i += 2 {
node := value.Content[i]
valueNode := value.Content[i+1]
if err := ex.decode(node, valueNode, location); err != nil {
return fmt.Errorf("decode example: %w", err)
}
}
n, err := ex.raml.makeRootNode(valueKey, location)
if err != nil {
return StacktraceNewWrapped("make node", err, location, WithNodePosition(valueKey))
}
ex.Data = n
return nil
}
// makeExample creates an example from the given value node
func (r *RAML) makeExample(value *yaml.Node, name string, location string) (*Example, error) {
ex := &Example{
Name: name,
Strict: true,
Location: location,
Position: stacktrace.Position{Line: value.Line, Column: value.Column},
CustomDomainProperties: orderedmap.New[string, *DomainExtension](0),
raml: r,
}
// Example can be represented as map in two cases:
// 1. A value with an example of ObjectShape.
// 2. A map with the required "value" key that contains the actual example and additional properties of Example.
if value.Kind == yaml.MappingNode {
err := ex.fill(location, value)
if err == nil {
return ex, nil
} else if !errors.Is(err, ErrValueKeyNotFound) {
return nil, fmt.Errorf("fill example from mapping node: %w", err)
}
}
// In all other cases, the example is considered as a value node
n, err := r.makeRootNode(value, location)
if err != nil {
return nil, StacktraceNewWrapped("make node", err, location, WithNodePosition(value))
}
ex.Data = n
return ex, nil
}
// Example represents an example of a shape
type Example struct {
ID string
Name string
DisplayName string
Description string
Data *Node
Strict bool
CustomDomainProperties *orderedmap.OrderedMap[string, *DomainExtension]
Location string
stacktrace.Position
raml *RAML
}