Skip to content

Commit 4540d88

Browse files
committed
refactor: pre-populate Draft 4 schemas to avoid network calls
Refs: #155
1 parent bd24969 commit 4540d88

File tree

2 files changed

+170
-4
lines changed

2 files changed

+170
-4
lines changed

src/main/kotlin/com/github/lppedd/cc/configuration/CCTokensService.kt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.intellij.openapi.vfs.LocalFileSystem
1111
import com.intellij.openapi.vfs.VirtualFile
1212
import org.codehaus.jettison.json.JSONArray
1313
import org.codehaus.jettison.json.JSONObject
14+
import java.io.Reader
15+
import java.net.URI
1416

1517
/**
1618
* Manages bundled and custom commit message tokens.
@@ -28,12 +30,27 @@ internal class CCTokensService(private val project: Project) {
2830
* JSON Schema used to validate the default commit types and scopes JSON file.
2931
*/
3032
private val tokensSchema: Schema by lazy {
31-
val bufferedReader = getResourceAsStream("/defaults/${CC.File.Schema}").bufferedReader()
32-
val schemaJson = bufferedReader.use {
33-
JsonParser(bufferedReader).parse()
33+
// See https://github.com/erosb/json-sKema/tree/master/src/main/resources/json-meta-schemas
34+
val draft04Reader = getResourceAsStream("/json-meta-schemas/draft04/schema.json").bufferedReader()
35+
val draft04Str = draft04Reader.use(Reader::readText)
36+
37+
val schemaReader = getResourceAsStream("/defaults/${CC.File.Schema}").bufferedReader()
38+
val schemaJson = schemaReader.use {
39+
JsonParser(schemaReader).parse()
3440
}
3541

36-
return@lazy SchemaLoader(schemaJson).load()
42+
// Pre-populate additional Draft 4 schemas to avoid network calls.
43+
// I'm not sure whether all of these are needed, but let's play on the safe side.
44+
val config = SchemaLoaderConfig.createDefaultConfig(
45+
mapOf(
46+
URI("http://json-schema.org/draft-04/schema") to draft04Str,
47+
URI("http://json-schema.org/draft-04/schema#") to draft04Str,
48+
URI("https://json-schema.org/draft-04/schema") to draft04Str,
49+
URI("https://json-schema.org/draft-04/schema#") to draft04Str,
50+
)
51+
)
52+
53+
return@lazy SchemaLoader(schemaJson, config).load()
3754
}
3855

3956
/**
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
{
2+
"id": "http://json-schema.org/draft-04/schema#",
3+
"$schema": "http://json-schema.org/draft-04/schema#",
4+
"description": "Core schema meta-schema",
5+
"definitions": {
6+
"schemaArray": {
7+
"type": "array",
8+
"minItems": 1,
9+
"items": { "$ref": "#" }
10+
},
11+
"positiveInteger": {
12+
"type": "integer",
13+
"minimum": 0
14+
},
15+
"positiveIntegerDefault0": {
16+
"allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
17+
},
18+
"simpleTypes": {
19+
"enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
20+
},
21+
"stringArray": {
22+
"type": "array",
23+
"items": { "type": "string" },
24+
"minItems": 1,
25+
"uniqueItems": true
26+
}
27+
},
28+
"type": "object",
29+
"properties": {
30+
"id": {
31+
"type": "string"
32+
},
33+
"$schema": {
34+
"type": "string"
35+
},
36+
"title": {
37+
"type": "string"
38+
},
39+
"description": {
40+
"type": "string"
41+
},
42+
"default": {},
43+
"multipleOf": {
44+
"type": "number",
45+
"minimum": 0,
46+
"exclusiveMinimum": true
47+
},
48+
"maximum": {
49+
"type": "number"
50+
},
51+
"exclusiveMaximum": {
52+
"type": "boolean",
53+
"default": false
54+
},
55+
"minimum": {
56+
"type": "number"
57+
},
58+
"exclusiveMinimum": {
59+
"type": "boolean",
60+
"default": false
61+
},
62+
"maxLength": { "$ref": "#/definitions/positiveInteger" },
63+
"minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
64+
"pattern": {
65+
"type": "string",
66+
"format": "regex"
67+
},
68+
"additionalItems": {
69+
"anyOf": [
70+
{ "type": "boolean" },
71+
{ "$ref": "#" }
72+
],
73+
"default": {}
74+
},
75+
"items": {
76+
"anyOf": [
77+
{ "$ref": "#" },
78+
{ "$ref": "#/definitions/schemaArray" }
79+
],
80+
"default": {}
81+
},
82+
"maxItems": { "$ref": "#/definitions/positiveInteger" },
83+
"minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
84+
"uniqueItems": {
85+
"type": "boolean",
86+
"default": false
87+
},
88+
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
89+
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
90+
"required": { "$ref": "#/definitions/stringArray" },
91+
"additionalProperties": {
92+
"anyOf": [
93+
{ "type": "boolean" },
94+
{ "$ref": "#" }
95+
],
96+
"default": {}
97+
},
98+
"definitions": {
99+
"type": "object",
100+
"additionalProperties": { "$ref": "#" },
101+
"default": {}
102+
},
103+
"properties": {
104+
"type": "object",
105+
"additionalProperties": { "$ref": "#" },
106+
"default": {}
107+
},
108+
"patternProperties": {
109+
"type": "object",
110+
"additionalProperties": { "$ref": "#" },
111+
"default": {}
112+
},
113+
"dependencies": {
114+
"type": "object",
115+
"additionalProperties": {
116+
"anyOf": [
117+
{ "$ref": "#" },
118+
{ "$ref": "#/definitions/stringArray" }
119+
]
120+
}
121+
},
122+
"enum": {
123+
"type": "array",
124+
"minItems": 1,
125+
"uniqueItems": true
126+
},
127+
"type": {
128+
"anyOf": [
129+
{ "$ref": "#/definitions/simpleTypes" },
130+
{
131+
"type": "array",
132+
"items": { "$ref": "#/definitions/simpleTypes" },
133+
"minItems": 1,
134+
"uniqueItems": true
135+
}
136+
]
137+
},
138+
"format": { "type": "string" },
139+
"allOf": { "$ref": "#/definitions/schemaArray" },
140+
"anyOf": { "$ref": "#/definitions/schemaArray" },
141+
"oneOf": { "$ref": "#/definitions/schemaArray" },
142+
"not": { "$ref": "#" }
143+
},
144+
"dependencies": {
145+
"exclusiveMaximum": [ "maximum" ],
146+
"exclusiveMinimum": [ "minimum" ]
147+
},
148+
"default": {}
149+
}

0 commit comments

Comments
 (0)