-
-
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
102 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) | ||
checkStringKey("github_username", isRequired = true) | ||
checkStringKey("exercism_username", isRequired = false) | ||
|
||
template isValidFiles(data: JsonNode, context, path: string) = | ||
checkObject(context) | ||
checkArrayOfStrings(context, "solution", isRequired = true) | ||
checkArrayOfStrings(context, "test", isRequired = true) | ||
checkArrayOfStrings(context, "exemplar", isRequired = true) | ||
|
||
proc isValidConceptExerciseConfig(data: JsonNode, path: string): bool = | ||
checkObject("root") | ||
checkArrayOf("authors", isValidAuthorOrContributor, isRequired = true) | ||
checkArrayOf("contributors", isValidAuthorOrContributor, isRequired = false) | ||
checkArrayOfStrings("", "forked_from", isRequired = false) | ||
isValidFiles(data, "files", path) | ||
checkStringKey("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,54 @@ | ||
import std/json | ||
import ".."/helpers | ||
|
||
template checkObject*(key: string) = | ||
if data.kind != JObject: | ||
writeError("Not an object: " & key, path) | ||
return false | ||
result = true | ||
|
||
template checkStringKey*(key: string, isRequired: bool) = | ||
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: bool) = | ||
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: string, path: string): bool, | ||
isRequired: bool) = | ||
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) |