1// Copyright 2024 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package jsonschema
16
17import (
18 "fmt"
19 "strings"
20)
21
22//go:generate go tool stringer -type=Version -linecomment
23
24type Version int
25
26const (
27 VersionUnknown Version = iota // unknown
28 VersionDraft4 // http://json-schema.org/draft-04/schema#
29 // Note: draft 5 never existed and should not be used.
30 VersionDraft6 // http://json-schema.org/draft-06/schema#
31 VersionDraft7 // http://json-schema.org/draft-07/schema#
32 VersionDraft2019_09 // https://json-schema.org/draft/2019-09/schema
33 VersionDraft2020_12 // https://json-schema.org/draft/2020-12/schema
34
35 numJSONSchemaVersions // unknown
36
37 // Note: The following versions stand alone: they're not in the regular JSON Schema lineage.
38 VersionOpenAPI // OpenAPI 3.0
39 VersionKubernetesAPI // Kubernetes API
40 VersionKubernetesCRD // Kubernetes CRD
41)
42
43const (
44 openAPI = versionSet(1 << VersionOpenAPI)
45 k8sAPI = versionSet(1 << VersionKubernetesAPI)
46 k8sCRD = versionSet(1 << VersionKubernetesCRD)
47 k8s = k8sAPI | k8sCRD
48 openAPILike = openAPI | k8s
49)
50
51// is reports whether v is in the set vs.
52func (v Version) is(vs versionSet) bool {
53 return vs.contains(v)
54}
55
56type versionSet int
57
58// allVersions includes all regular versions of JSON Schema.
59// It does not include OpenAPI v3.0
60const allVersions = versionSet(1<<numJSONSchemaVersions-1) &^ (1 << VersionUnknown)
61
62// contains reports whether m contains the version v.
63func (m versionSet) contains(v Version) bool {
64 return (m & vset(v)) != 0
65}
66
67// vset returns the version set containing exactly v.
68func vset(v Version) versionSet {
69 return 1 << v
70}
71
72// vfrom returns the set of all versions starting at v.
73func vfrom(v Version) versionSet {
74 return allVersions &^ (vset(v) - 1)
75}
76
77// vbetween returns the set of all versions between
78// v0 and v1 inclusive.
79func vbetween(v0, v1 Version) versionSet {
80 return vfrom(v0) & vto(v1)
81}
82
83// vto returns the set of all versions up to
84// and including v.
85func vto(v Version) versionSet {
86 return allVersions & (vset(v+1) - 1)
87}
88
89// ParseVersion parses a version URI that defines a JSON Schema version.
90func ParseVersion(sv string) (Version, error) {
91 // Ignore a trailing empty fragment: it's a common error
92 // to omit or supply such a fragment and it's not entirely
93 // clear whether comparison should or should not
94 // be sensitive to its presence or absence.
95 sv = strings.TrimSuffix(sv, "#")
96 // If this linear search is ever a performance issue, we could
97 // build a map, but it doesn't seem worthwhile for now.
98 for i := Version(1); i < numJSONSchemaVersions; i++ {
99 if sv == strings.TrimSuffix(i.String(), "#") {
100 return i, nil
101 }
102 }
103 return 0, fmt.Errorf("$schema URI not recognized")
104}