-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lint: begin linting concept exercise
config.json
- Loading branch information
Showing
4 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import std/[json, os, terminal] | ||
import ".."/helpers | ||
import "."/validators | ||
|
||
proc isValidAuthorOrContributor(data: JsonNode, key: string, path: string): bool = | ||
checkObject(key) | ||
checkString("github_username") | ||
checkString("exercism_username") | ||
|
||
template checkFiles(data: JsonNode, context, path: string) = | ||
checkObject(context) | ||
checkArrayOfStrings(context, "solution") | ||
checkArrayOfStrings(context, "test") | ||
checkArrayOfStrings(context, "exemplar") | ||
|
||
proc isValidConceptExerciseConfig(data: JsonNode, path: string): bool = | ||
checkObject("") | ||
checkArrayOf("authors", isValidAuthorOrContributor) | ||
checkArrayOf("contributors", isValidAuthorOrContributor, isRequired = false) | ||
checkFiles(data, "files", path) | ||
checkArrayOfStrings("", "forked_from", isRequired = false) | ||
checkString("language_versions", isRequired = false) | ||
|
||
proc isEveryConceptExerciseConfigValid*(trackDir: string): bool = | ||
let conceptExercisesDir = trackDir / "exercises" / "concept" | ||
result = true | ||
if dirExists(conceptExercisesDir): | ||
for exerciseDir in getSortedSubdirs(conceptExercisesDir): | ||
let configPath = exerciseDir / ".meta" / "config.json" | ||
let j = | ||
try: | ||
parseFile(configPath) | ||
except: | ||
writeError("JSON parsing error", getCurrentExceptionMsg()) | ||
continue | ||
if not isValidConceptExerciseConfig(j, configPath): | ||
result = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import std/json | ||
import ".."/helpers | ||
|
||
template checkObject*(key: string) = | ||
if key.len == 0: | ||
if data.kind != JObject: | ||
writeError("JSON root is not an object", path) | ||
return false | ||
elif data.hasKey(key): | ||
if data[key].kind != JObject: | ||
writeError("Not an object: " & key, path) | ||
return false | ||
else: | ||
writeError("Missing key: " & key, path) | ||
return false | ||
result = true | ||
|
||
template checkString*(key: string, isRequired = true) = | ||
if data.hasKey(key): | ||
if data[key].kind == JString: | ||
if data[key].getStr().len == 0: | ||
writeError("String is zero-length: " & key, path) | ||
else: | ||
writeError("Not a string: `" & key & ": " & $data[key] & "`", path) | ||
elif isRequired: | ||
writeError("Missing key: " & key, path) | ||
|
||
template checkArrayOfStrings*(context, key: string; isRequired = true) = | ||
if data.hasKey(key): | ||
if data[key].kind == JArray: | ||
if data[key].len == 0: | ||
writeError("Array is empty: " & key, path) | ||
else: | ||
for item in data[key]: | ||
if item.kind != JString: | ||
result = false | ||
# writeError("Array contains item with the wrong kind" & key & $item, path) | ||
# break | ||
elif item.getStr().len == 0: | ||
writeError("Array contains zero-length string: " & key, path) | ||
else: | ||
writeError("Not an array: " & key, path) | ||
elif isRequired: | ||
writeError("Missing key: " & context & "." & key, path) | ||
|
||
template checkArrayOf*(key: string, | ||
call: proc(d: JsonNode; key, path: string): bool, | ||
isRequired = true) = | ||
if data.hasKey(key): | ||
if data[key].kind == JArray: | ||
if data[key].len == 0: | ||
writeError("Array is empty: " & key, path) | ||
else: | ||
for item in data[key]: | ||
if not call(item, key, path): | ||
result = false | ||
# writeError("Item in array fails check: " & $item, path) | ||
# break | ||
else: | ||
writeError("Not an array: " & key, path) | ||
elif isRequired: | ||
writeError("Missing key: " & key, path) |