This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from datacontract/datacontract-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.go
177 lines (141 loc) · 3.76 KB
/
reference.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package datacontract
import (
"errors"
"fmt"
"gopkg.in/yaml.v3"
"io"
"net/http"
"os"
"reflect"
"strings"
)
const StringReferencePrefix = "$ref:"
const ObjectReferenceKey = "$ref"
func IsReference(reference any) bool {
if text, ok := reference.(string); ok {
return strings.HasPrefix(text, StringReferencePrefix)
}
if obj, ok := reference.(map[string]any); ok {
return obj[ObjectReferenceKey] != nil
}
return false
}
func ResolveReference(contract DataContract, reference any) (_ any, err error) {
if text, ok := reference.(string); ok {
return resolveStringReference(text)
}
if obj, ok := reference.(map[string]any); ok {
if objReference, ok := obj[ObjectReferenceKey].(string); ok {
return resolveObjectReference(objReference, contract)
}
}
return nil, fmt.Errorf("can't resolve reference for type %v", reflect.TypeOf(reference))
}
func resolveStringReference(reference string) (any, error) {
cleanReference := strings.Trim(strings.TrimPrefix(reference, StringReferencePrefix), " ")
bytes, err := resolveFile(cleanReference)
if err != nil {
return "", err
}
return string(bytes), nil
}
func resolveObjectReference(reference string, contract DataContract) (any, error) {
segments := strings.Split(reference, "#")
document, err := resolveDocument(segments, contract)
if err != nil {
return nil, err
}
path := getPath(segments)
// prevent accidentally referencing root in same file
if strings.HasPrefix(reference, "#") && len(path) < 1 {
return nil, nil
}
// use GetValue to get value inside referenced document
field, err := GetValue(document, path)
if err != nil {
return nil, err
}
if anyMap, ok := field.(map[string]any); !ok {
return nil, errors.New("referenced value is not an object")
} else {
return anyMap, nil
}
}
func getPath(segments []string) []string {
if len(segments) < 2 {
return []string{}
} else {
pathString := strings.TrimPrefix(segments[1], "/")
split := strings.Split(pathString, "/")
var path []string
for _, s := range split {
if s != "" {
path = append(path, s)
}
}
return path
}
}
func resolveDocument(segments []string, contract DataContract) (map[string]any, error) {
if segments[0] == "" {
return contract, nil
} else {
bytes, err := resolveFile(segments[0])
if err != nil {
return nil, err
}
objectFromFile := map[string]any{}
err = yaml.Unmarshal(bytes, objectFromFile)
if err != nil {
return nil, err
}
return objectFromFile, nil
}
}
func resolveFile(reference string) (bytes []byte, err error) {
if IsURI(reference) {
bytes, err = resolveFileFromRemote(reference)
} else {
bytes, err = resolveFileLocally(reference)
}
if err != nil {
return nil, fmt.Errorf("can't resolve reference '%v': %w", reference, err)
}
return bytes, nil
}
func resolveFileLocally(path string) ([]byte, error) {
return os.ReadFile(path)
}
func resolveFileFromRemote(url string) ([]byte, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
return io.ReadAll(response.Body)
}
func InlineReferences(item *map[string]any, contract DataContract) error {
for key, field := range *item {
if IsReference(field) {
value, err := ResolveReference(contract, field)
if err != nil {
return err
}
// also resolve references inside references
if object, isObject := value.(map[string]any); isObject {
InlineReferences(&object, contract)
}
object := *item
object[key] = value
} else if object, isObject := field.(map[string]any); isObject {
InlineReferences(&object, contract)
} else if list, isList := field.([]any); isList {
for _, item := range list {
if object, isObject := item.(map[string]any); isObject {
InlineReferences(&object, contract)
}
}
}
}
return nil
}