@@ -16,7 +16,6 @@ package swag
16
16
17
17
import (
18
18
"encoding/json"
19
- "errors"
20
19
"fmt"
21
20
"path/filepath"
22
21
"reflect"
@@ -51,7 +50,7 @@ func BytesToYAMLDoc(data []byte) (interface{}, error) {
51
50
return nil , err
52
51
}
53
52
if document .Kind != yaml .DocumentNode || len (document .Content ) != 1 || document .Content [0 ].Kind != yaml .MappingNode {
54
- return nil , errors . New ("only YAML documents that are objects are supported" )
53
+ return nil , fmt . Errorf ("only YAML documents that are objects are supported: %w" , ErrYAML )
55
54
}
56
55
return & document , nil
57
56
}
@@ -69,31 +68,32 @@ func yamlNode(root *yaml.Node) (interface{}, error) {
69
68
case yaml .AliasNode :
70
69
return yamlNode (root .Alias )
71
70
default :
72
- return nil , fmt .Errorf ("unsupported YAML node type: %v" , root .Kind )
71
+ return nil , fmt .Errorf ("unsupported YAML node type: %v: %w " , root .Kind , ErrYAML )
73
72
}
74
73
}
75
74
76
75
func yamlDocument (node * yaml.Node ) (interface {}, error ) {
77
76
if len (node .Content ) != 1 {
78
- return nil , fmt .Errorf ("unexpected YAML Document node content length: %d" , len (node .Content ))
77
+ return nil , fmt .Errorf ("unexpected YAML Document node content length: %d: %w " , len (node .Content ), ErrYAML )
79
78
}
80
79
return yamlNode (node .Content [0 ])
81
80
}
82
81
83
82
func yamlMapping (node * yaml.Node ) (interface {}, error ) {
84
- m := make (JSONMapSlice , len (node .Content )/ 2 )
83
+ const sensibleAllocDivider = 2
84
+ m := make (JSONMapSlice , len (node .Content )/ sensibleAllocDivider )
85
85
86
86
var j int
87
87
for i := 0 ; i < len (node .Content ); i += 2 {
88
88
var nmi JSONMapItem
89
89
k , err := yamlStringScalarC (node .Content [i ])
90
90
if err != nil {
91
- return nil , fmt .Errorf ("unable to decode YAML map key: %w" , err )
91
+ return nil , fmt .Errorf ("unable to decode YAML map key: %w: %w " , err , ErrYAML )
92
92
}
93
93
nmi .Key = k
94
94
v , err := yamlNode (node .Content [i + 1 ])
95
95
if err != nil {
96
- return nil , fmt .Errorf ("unable to process YAML map value for key %q: %w" , k , err )
96
+ return nil , fmt .Errorf ("unable to process YAML map value for key %q: %w: %w " , k , err , ErrYAML )
97
97
}
98
98
nmi .Value = v
99
99
m [j ] = nmi
@@ -109,7 +109,7 @@ func yamlSequence(node *yaml.Node) (interface{}, error) {
109
109
110
110
v , err := yamlNode (node .Content [i ])
111
111
if err != nil {
112
- return nil , fmt .Errorf ("unable to decode YAML sequence value: %w" , err )
112
+ return nil , fmt .Errorf ("unable to decode YAML sequence value: %w: %w " , err , ErrYAML )
113
113
}
114
114
s = append (s , v )
115
115
}
@@ -132,39 +132,39 @@ func yamlScalar(node *yaml.Node) (interface{}, error) {
132
132
case yamlBoolScalar :
133
133
b , err := strconv .ParseBool (node .Value )
134
134
if err != nil {
135
- return nil , fmt .Errorf ("unable to process scalar node. Got %q. Expecting bool content: %w" , node .Value , err )
135
+ return nil , fmt .Errorf ("unable to process scalar node. Got %q. Expecting bool content: %w: %w " , node .Value , err , ErrYAML )
136
136
}
137
137
return b , nil
138
138
case yamlIntScalar :
139
139
i , err := strconv .ParseInt (node .Value , 10 , 64 )
140
140
if err != nil {
141
- return nil , fmt .Errorf ("unable to process scalar node. Got %q. Expecting integer content: %w" , node .Value , err )
141
+ return nil , fmt .Errorf ("unable to process scalar node. Got %q. Expecting integer content: %w: %w " , node .Value , err , ErrYAML )
142
142
}
143
143
return i , nil
144
144
case yamlFloatScalar :
145
145
f , err := strconv .ParseFloat (node .Value , 64 )
146
146
if err != nil {
147
- return nil , fmt .Errorf ("unable to process scalar node. Got %q. Expecting float content: %w" , node .Value , err )
147
+ return nil , fmt .Errorf ("unable to process scalar node. Got %q. Expecting float content: %w: %w " , node .Value , err , ErrYAML )
148
148
}
149
149
return f , nil
150
150
case yamlTimestamp :
151
151
return node .Value , nil
152
152
case yamlNull :
153
153
return nil , nil //nolint:nilnil
154
154
default :
155
- return nil , fmt .Errorf ("YAML tag %q is not supported" , node .LongTag ())
155
+ return nil , fmt .Errorf ("YAML tag %q is not supported: %w " , node .LongTag (), ErrYAML )
156
156
}
157
157
}
158
158
159
159
func yamlStringScalarC (node * yaml.Node ) (string , error ) {
160
160
if node .Kind != yaml .ScalarNode {
161
- return "" , fmt .Errorf ("expecting a string scalar but got %q" , node .Kind )
161
+ return "" , fmt .Errorf ("expecting a string scalar but got %q: %w " , node .Kind , ErrYAML )
162
162
}
163
163
switch node .LongTag () {
164
164
case yamlStringScalar , yamlIntScalar , yamlFloatScalar :
165
165
return node .Value , nil
166
166
default :
167
- return "" , fmt .Errorf ("YAML tag %q is not supported as map key" , node .LongTag ())
167
+ return "" , fmt .Errorf ("YAML tag %q is not supported as map key: %w " , node .LongTag (), ErrYAML )
168
168
}
169
169
}
170
170
@@ -349,7 +349,7 @@ func json2yaml(item interface{}) (*yaml.Node, error) {
349
349
Value : strconv .FormatBool (val ),
350
350
}, nil
351
351
default :
352
- return nil , fmt .Errorf ("unhandled type: %T" , val )
352
+ return nil , fmt .Errorf ("unhandled type: %T: %w " , val , ErrYAML )
353
353
}
354
354
}
355
355
@@ -416,7 +416,7 @@ func transformData(input interface{}) (out interface{}, err error) {
416
416
case int64 :
417
417
return strconv .FormatInt (k , 10 ), nil
418
418
default :
419
- return "" , fmt .Errorf ("unexpected map key type, got: %T" , k )
419
+ return "" , fmt .Errorf ("unexpected map key type, got: %T: %w " , k , ErrYAML )
420
420
}
421
421
}
422
422
0 commit comments