-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.go
More file actions
169 lines (148 loc) · 4.75 KB
/
constants.go
File metadata and controls
169 lines (148 loc) · 4.75 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
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
package schema
import (
"fmt"
"sort"
)
const (
ModelContractName = "io.mb3r.bering.model"
SnapshotContractName = "io.mb3r.bering.snapshot"
ContractVersionV1_0_0 = "1.0.0"
ContractVersionV1_1_0 = "1.1.0"
LatestContractVersion = ContractVersionV1_1_0
ExpectedSchemaName = ModelContractName
ExpectedSchemaVersion = LatestContractVersion
ExpectedSchemaURI = "https://mb3r-lab.github.io/Bering/schema/model/v1.1.0/model.schema.json"
ExpectedSchemaDigest = "sha256:bc9a60736c9e6bda9599243fd68f293b88f42ade65321d8267369a5c3214779a"
ExpectedSnapshotSchemaName = SnapshotContractName
ExpectedSnapshotSchemaVersion = LatestContractVersion
ExpectedSnapshotSchemaURI = "https://mb3r-lab.github.io/Bering/schema/snapshot/v1.1.0/snapshot.schema.json"
ExpectedSnapshotSchemaDigest = "sha256:53b127608b2aaa4fabb352b998cd6b2c5ed558764729a09abea56f4f9b40fa01"
)
type SchemaRef struct {
Name string `json:"name"`
Version string `json:"version"`
URI string `json:"uri"`
Digest string `json:"digest"`
}
type PublishedContract struct {
Ref SchemaRef
Artifact string
APIPath string
EmbeddedPath string
}
var publishedContracts = []PublishedContract{
{
Ref: SchemaRef{
Name: ModelContractName,
Version: ContractVersionV1_0_0,
URI: "https://mb3r-lab.github.io/Bering/schema/model/v1.0.0/model.schema.json",
Digest: "sha256:272277c093f37580adcd2dded225bd37c86539d642d7910baad7e4228227d1a7",
},
Artifact: "model",
APIPath: "api/schema/model/v1.0.0/model.schema.json",
EmbeddedPath: "schema/model/v1.0.0/model.schema.json",
},
{
Ref: SchemaRef{
Name: SnapshotContractName,
Version: ContractVersionV1_0_0,
URI: "https://mb3r-lab.github.io/Bering/schema/snapshot/v1.0.0/snapshot.schema.json",
Digest: "sha256:87e4e887ed4a37b72f6136e268b73552eccb92941c4de2c6f3a514dd066ea972",
},
Artifact: "snapshot",
APIPath: "api/schema/snapshot/v1.0.0/snapshot.schema.json",
EmbeddedPath: "schema/snapshot/v1.0.0/snapshot.schema.json",
},
{
Ref: SchemaRef{
Name: ModelContractName,
Version: ContractVersionV1_1_0,
URI: ExpectedSchemaURI,
Digest: ExpectedSchemaDigest,
},
Artifact: "model",
APIPath: "api/schema/model/v1.1.0/model.schema.json",
EmbeddedPath: "schema/model/v1.1.0/model.schema.json",
},
{
Ref: SchemaRef{
Name: SnapshotContractName,
Version: ContractVersionV1_1_0,
URI: ExpectedSnapshotSchemaURI,
Digest: ExpectedSnapshotSchemaDigest,
},
Artifact: "snapshot",
APIPath: "api/schema/snapshot/v1.1.0/snapshot.schema.json",
EmbeddedPath: "schema/snapshot/v1.1.0/snapshot.schema.json",
},
}
var publishedByKey = buildPublishedByKey(publishedContracts)
func ExpectedRef() SchemaRef {
return mustRef(ModelContractName, LatestContractVersion)
}
func ExpectedSnapshotRef() SchemaRef {
return mustRef(SnapshotContractName, LatestContractVersion)
}
func SupportedContractVersions() []string {
versions := []string{ContractVersionV1_0_0, ContractVersionV1_1_0}
sort.Strings(versions)
return versions
}
func PublishedContracts() []PublishedContract {
out := make([]PublishedContract, len(publishedContracts))
copy(out, publishedContracts)
return out
}
func LookupContract(name, version string) (PublishedContract, bool) {
contract, ok := publishedByKey[schemaKey(name, version)]
return contract, ok
}
func ModelRef(version string) (SchemaRef, bool) {
contract, ok := LookupContract(ModelContractName, version)
if !ok {
return SchemaRef{}, false
}
return contract.Ref, true
}
func SnapshotRef(version string) (SchemaRef, bool) {
contract, ok := LookupContract(SnapshotContractName, version)
if !ok {
return SchemaRef{}, false
}
return contract.Ref, true
}
func ResolveContractLine(version string) (SchemaRef, SchemaRef, error) {
modelRef, ok := ModelRef(version)
if !ok {
return SchemaRef{}, SchemaRef{}, fmt.Errorf("unsupported model contract version: %s", version)
}
snapshotRef, ok := SnapshotRef(version)
if !ok {
return SchemaRef{}, SchemaRef{}, fmt.Errorf("unsupported snapshot contract version: %s", version)
}
return modelRef, snapshotRef, nil
}
func mustRef(name, version string) SchemaRef {
ref, ok := lookupRef(name, version)
if !ok {
panic(fmt.Sprintf("unknown schema ref %s@%s", name, version))
}
return ref
}
func lookupRef(name, version string) (SchemaRef, bool) {
contract, ok := LookupContract(name, version)
if !ok {
return SchemaRef{}, false
}
return contract.Ref, true
}
func buildPublishedByKey(items []PublishedContract) map[string]PublishedContract {
out := make(map[string]PublishedContract, len(items))
for _, item := range items {
out[schemaKey(item.Ref.Name, item.Ref.Version)] = item
}
return out
}
func schemaKey(name, version string) string {
return name + "@" + version
}