-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarsh.go
More file actions
39 lines (32 loc) · 1.11 KB
/
marsh.go
File metadata and controls
39 lines (32 loc) · 1.11 KB
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
package openapi3
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/oasdiff/yaml"
)
func unmarshalError(jsonUnmarshalErr error) error {
if before, after, found := strings.Cut(jsonUnmarshalErr.Error(), "Bis"); found && before != "" && after != "" {
before = strings.ReplaceAll(before, " Go struct ", " ")
return fmt.Errorf("%s%s", before, strings.ReplaceAll(after, "Bis", ""))
}
return jsonUnmarshalErr
}
func unmarshal(data []byte, v any, includeOrigin bool, location *url.URL) error {
var jsonErr, yamlErr error
// See https://github.com/getkin/kin-openapi/issues/680
if jsonErr = json.Unmarshal(data, v); jsonErr == nil {
return nil
}
// UnmarshalStrict(data, v) TODO: investigate how ymlv3 handles duplicate map keys
var file string
if location != nil {
file = location.Path
}
if yamlErr = yaml.UnmarshalWithOrigin(data, v, yaml.OriginOpt{Enabled: includeOrigin, File: file}); yamlErr == nil {
return nil
}
// If both unmarshaling attempts fail, return a new error that includes both errors
return fmt.Errorf("failed to unmarshal data: json error: %v, yaml error: %v", jsonErr, yamlErr)
}