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
/
dataContract.go
115 lines (94 loc) · 2.5 KB
/
dataContract.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
package datacontract
import (
"fmt"
"gopkg.in/yaml.v3"
"io"
"net/http"
"os"
)
type DataContract = map[string]any
func GetDataContract(location string) (DataContract, error) {
if IsURI(location) {
return getRemoteDataContract(location)
} else {
return getLocalDataContract(location)
}
}
func getLocalDataContract(dataContractFileName string) (DataContract, error) {
if dataContractFile, err := os.ReadFile(dataContractFileName); err != nil {
return nil, fmt.Errorf("failed to read data contract file: %w", err)
} else {
return parseDataContract(dataContractFile)
}
}
func getRemoteDataContract(url string) (DataContract, error) {
response, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch data contract to compare with: %v", response.Status)
}
defer response.Body.Close()
if contractData, err := io.ReadAll(response.Body); err != nil {
return nil, fmt.Errorf("failed to read data contract to compare with: %w", err)
} else {
return parseDataContract(contractData)
}
}
func parseDataContract(data []byte) (dataContractObject DataContract, err error) {
if err = yaml.Unmarshal(data, &dataContractObject); err != nil {
return nil, fmt.Errorf("failed to unmarshal data contract file: %w", err)
}
return dataContractObject, nil
}
func SetValue(contract DataContract, path []string, value any) {
applyOnField(
contract,
path,
true,
func(object map[string]any, fieldName string) (any, error) {
object[fieldName] = value
return nil, nil
},
)
}
func GetValue(contract DataContract, path []string) (value any, err error) {
return applyOnField(
contract,
path,
false,
func(object map[string]any, fieldName string) (any, error) {
field := object[fieldName]
if field == nil {
return nil, fmt.Errorf("no field named '%v'", fieldName)
}
if IsReference(field) {
field, err = ResolveReference(contract, field)
if err != nil {
return nil, err
}
}
return field, nil
},
)
}
func applyOnField(
object map[string]any,
path []string,
pave bool,
do func(object map[string]any, fieldName string) (any, error),
) (any, error) {
if len(path) < 1 {
return object, nil
}
fieldName := path[0]
if object[fieldName] == nil && pave {
object[fieldName] = map[string]any{}
}
if len(path) == 1 {
return do(object, fieldName)
}
next, ok := object[fieldName].(map[string]any)
if !ok {
return nil, fmt.Errorf("can't follow path using field '%v', it's not a map", fieldName)
}
return applyOnField(next, path[1:], pave, do)
}