forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONSchema.swift
More file actions
167 lines (141 loc) · 5.71 KB
/
JSONSchema.swift
File metadata and controls
167 lines (141 loc) · 5.71 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
import Foundation
public struct JSONSchemaKey: Codable, Hashable, Sendable, Equatable, ExpressibleByStringLiteral {
public var key: String
public init(stringLiteral: String) {
key = stringLiteral
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(key)
}
public init(from decoder: Decoder) throws {
let single = try? decoder.singleValueContainer()
if let value = try? single?.decode(String.self) {
key = value
return
}
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "failed to decode JSON schema key"))
}
public static let type: JSONSchemaKey = "type"
public static let minLength: JSONSchemaKey = "minLength"
public static let maxLength: JSONSchemaKey = "maxLength"
public static let pattern: JSONSchemaKey = "pattern"
public static let format: JSONSchemaKey = "format"
public static let multipleOf: JSONSchemaKey = "multipleOf"
public static let minimum: JSONSchemaKey = "minimum"
public static let exclusiveMinimum: JSONSchemaKey = "exclusiveMinimum"
public static let maximum: JSONSchemaKey = "maximum"
public static let exclusiveMaximum: JSONSchemaKey = "exclusiveMaximum"
public static let minProperties: JSONSchemaKey = "minProperties"
public static let maxProperties: JSONSchemaKey = "maxProperties"
public static let required: JSONSchemaKey = "required"
public static let properties: JSONSchemaKey = "properties"
public static let patternProperties: JSONSchemaKey = "patternProperties"
public static let additionalProperties: JSONSchemaKey = "additionalProperties"
public static let dependencies: JSONSchemaKey = "dependencies"
public static let propertyNames: JSONSchemaKey = "propertyNames"
public static let minItems: JSONSchemaKey = "minItems"
public static let maxItems: JSONSchemaKey = "maxItems"
public static let uniqueItems: JSONSchemaKey = "uniqueItems"
public static let items: JSONSchemaKey = "items"
public static let additionalItems: JSONSchemaKey = "additionalItems"
public static let contains: JSONSchemaKey = "contains"
public static let `enum`: JSONSchemaKey = "enum"
public static let const: JSONSchemaKey = "const"
public static let title: JSONSchemaKey = "title"
public static let description: JSONSchemaKey = "description"
public static let `default`: JSONSchemaKey = "default"
public static let examples: JSONSchemaKey = "examples"
public static let comment: JSONSchemaKey = "$comment"
public static let allOf: JSONSchemaKey = "allOf"
public static let anyOf: JSONSchemaKey = "anyOf"
public static let oneOf: JSONSchemaKey = "oneOf"
public static let not: JSONSchemaKey = "not"
public static let `if`: JSONSchemaKey = "if"
public static let then: JSONSchemaKey = "then"
public static let `else`: JSONSchemaKey = "else"
}
public enum JSONSchemaValue: Codable, Hashable, Sendable {
case bool(Bool)
case number(Double)
case string(String)
case array([JSONSchemaValue])
case hash([String: JSONSchemaValue])
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .bool(let value):
try container.encode(value)
case .number(let value):
try container.encode(value)
case .string(let value):
try container.encode(value)
case .array(let value):
try container.encode(value)
case .hash(let value):
try container.encode(value)
}
}
public init(from decoder: Decoder) throws {
let single = try? decoder.singleValueContainer()
if let value = try? single?.decode([String: JSONSchemaValue].self) {
self = .hash(value)
return
}
if let value = try? single?.decode([JSONSchemaValue].self) {
self = .array(value)
return
}
if let value = try? single?.decode(String.self) {
self = .string(value)
return
}
if let value = try? single?.decode(Double.self) {
self = .number(value)
return
}
if let value = try? single?.decode(Bool.self) {
self = .bool(value)
return
}
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "failed to decode JSON schema object"))
}
}
extension JSONSchemaValue: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (JSONSchemaKey, JSONSchemaValue)...) {
var hash = [String: JSONSchemaValue]()
for element in elements {
hash[element.0.key] = element.1
}
self = .hash(hash)
}
}
extension JSONSchemaValue: ExpressibleByStringLiteral {
public init(stringLiteral: String) {
self = .string(stringLiteral)
}
}
extension JSONSchemaValue: ExpressibleByIntegerLiteral {
public init(integerLiteral value: IntegerLiteralType) {
self = .number(Double(value))
}
}
extension JSONSchemaValue: ExpressibleByFloatLiteral {
public init(floatLiteral value: FloatLiteralType) {
self = .number(value)
}
}
extension JSONSchemaValue: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: JSONSchemaValue...) {
var array = [JSONSchemaValue]()
for element in elements {
array.append(element)
}
self = .array(array)
}
}
extension JSONSchemaValue: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: BooleanLiteralType) {
self = .bool(value)
}
}