From 2e1c56d745a208ab54c89f67efda60dcd637ee5c Mon Sep 17 00:00:00 2001 From: stirante Date: Wed, 28 Sep 2022 09:36:37 +0200 Subject: [PATCH] Remove old jsonc library and allow for comments at the top of the file --- go.mod | 1 - go.sum | 2 -- jsonte/functions/minecraft_functions.go | 25 +++++++++++------------- jsonte/utils/json_file.go | 26 ++++++++++++++----------- jsonte/utils/json_utils.go | 4 ++-- jsonte/utils/semver.go | 2 +- test/json_test.go | 2 +- 7 files changed, 30 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index 6088b99..5b88809 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/fatih/color v1.13.0 github.com/gammazero/deque v0.2.0 github.com/gobwas/glob v0.2.3 - github.com/stirante/jsonc v0.0.0-20220913161200-c4ec34866fdd go.uber.org/zap v1.23.0 golang.org/x/text v0.3.7 ) diff --git a/go.sum b/go.sum index 470ecd3..e6d0cd5 100644 --- a/go.sum +++ b/go.sum @@ -48,8 +48,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stirante/jsonc v0.0.0-20220913161200-c4ec34866fdd h1:+s6imB5Hhq+xYfNn+KIIbYpU6jUGr8MNqwu9ZzofsMk= -github.com/stirante/jsonc v0.0.0-20220913161200-c4ec34866fdd/go.mod h1:cZCwzwRtBT6mI2NyjisNOQHJWSbEVjUASlGxQFgrU1w= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= diff --git a/jsonte/functions/minecraft_functions.go b/jsonte/functions/minecraft_functions.go index 128225d..87792ae 100644 --- a/jsonte/functions/minecraft_functions.go +++ b/jsonte/functions/minecraft_functions.go @@ -4,7 +4,6 @@ import ( "archive/zip" "github.com/MCDevKit/jsonte/jsonte/safeio" "github.com/MCDevKit/jsonte/jsonte/utils" - "github.com/stirante/jsonc" "io" "io/ioutil" "os" @@ -413,22 +412,20 @@ func findPackVersions(isBp bool, uuid string) (utils.NavigableMap[string, string } return versions, utils.WrapErrorf(err, "Failed to read manifest.json in %s", p) } - var manifest map[string]interface{} - err = jsonc.Unmarshal(f, &manifest) + var manifest utils.NavigableMap[string, interface{}] + manifest, err = utils.ParseJson(f) if err != nil { return versions, utils.WrapErrorf(err, "Failed to parse manifest.json in %s", p) } - if header, ok := manifest["header"]; ok { - if headerMap, ok := header.(map[string]interface{}); ok { - if headerMap["uuid"] != uuid { - continue - } - if version, ok := headerMap["version"]; ok { - if versionArr, ok := version.([]interface{}); ok { - array := utils.ParseSemverArray(versionArr) - versions.Put(array.String(), p) - } - } + if manifest.ContainsKey("header") { + header := manifest.Get("header").(utils.NavigableMap[string, interface{}]) + if header.Get("uuid") != uuid { + continue + } + if header.ContainsKey("version") { + version := utils.UnwrapContainers(header.Get("version")).([]interface{}) + array := utils.ParseSemverArray(version) + versions.Put(array.String(), p) } } } diff --git a/jsonte/utils/json_file.go b/jsonte/utils/json_file.go index 8109263..48153cf 100644 --- a/jsonte/utils/json_file.go +++ b/jsonte/utils/json_file.go @@ -245,12 +245,20 @@ func isWhitespace(token byte) bool { } func skipWhitespace(str *StringReader) { + canStartComment := false for { token := str.Peek() - if !isWhitespace(token) { - break + if isWhitespace(token) { + str.Read() + continue + } else if token == TokenSlash { + canStartComment = parseComment(str, canStartComment) + continue + } else if token == TokenAsterisk { + canStartComment = parseComment(str, canStartComment) + continue } - str.Read() + break } } @@ -271,11 +279,9 @@ func parseObject(str *StringReader, p string) (NavigableMap[string, interface{}] return result, WrappedJsonErrorf(p, "Unexpected end of file at line %d, column %d", str.line, str.column) } else if isWhitespace(token) { str.Read() - } else if token == TokenSlash && !canStartComment { + } else if token == TokenSlash { canStartComment = parseComment(str, canStartComment) - } else if token == TokenSlash && canStartComment { - canStartComment = parseComment(str, canStartComment) - } else if token == TokenAsterisk && canStartComment { + } else if token == TokenAsterisk { canStartComment = parseComment(str, canStartComment) } else if token == TokenDoubleQuote && (comma || open) { comma = false @@ -459,11 +465,9 @@ func parseArray(str *StringReader, p string) ([]interface{}, error) { return result, WrappedJsonErrorf(p, "Unexpected token at line %d, column %d", str.line, str.column) } else if isWhitespace(token) { str.Read() - } else if token == TokenSlash && !canStartComment { - canStartComment = parseComment(str, canStartComment) - } else if token == TokenSlash && canStartComment { + } else if token == TokenSlash { canStartComment = parseComment(str, canStartComment) - } else if token == TokenAsterisk && canStartComment { + } else if token == TokenAsterisk { canStartComment = parseComment(str, canStartComment) } else if token == TokenOpenArray && (comma || open) { value, err := parseArray(str, fmt.Sprintf("%s[%d]", p, len(result))) diff --git a/jsonte/utils/json_utils.go b/jsonte/utils/json_utils.go index 9e35136..6a54d91 100644 --- a/jsonte/utils/json_utils.go +++ b/jsonte/utils/json_utils.go @@ -213,7 +213,7 @@ func ToString(obj interface{}) string { if b, ok := obj.(string); ok { return b } - str, err := MarshalJSONCObject(UnwrapContainers(obj).(NavigableMap[string, interface{}]), false, false) + str, err := MarshalJSONCObject(UnwrapContainers(obj).(NavigableMap[string, interface{}]), false) if err != nil { return "null" } @@ -246,7 +246,7 @@ func ToPrettyString(obj interface{}) string { if b, ok := obj.(string); ok { return b } - str, err := MarshalJSONCObject(UnwrapContainers(obj).(NavigableMap[string, interface{}]), true, true) + str, err := MarshalJSONCObject(UnwrapContainers(obj).(NavigableMap[string, interface{}]), true) if err != nil { return "null" } diff --git a/jsonte/utils/semver.go b/jsonte/utils/semver.go index 0739369..ad4b1e5 100644 --- a/jsonte/utils/semver.go +++ b/jsonte/utils/semver.go @@ -51,5 +51,5 @@ func ParseSemverString(version string) (Semver, error) { // ParseSemverArray parses an array representation of a semantic version func ParseSemverArray(version []interface{}) Semver { - return Semver{int(version[0].(float64)), int(version[1].(float64)), int(version[2].(float64))} + return Semver{int(version[0].(int32)), int(version[1].(int32)), int(version[2].(int32))} } diff --git a/test/json_test.go b/test/json_test.go index eb52b16..d8d83cf 100644 --- a/test/json_test.go +++ b/test/json_test.go @@ -580,7 +580,7 @@ func TestModuleOverride(t *testing.T) { } func TestJsonParser(t *testing.T) { - template := `{ + template := `/*comment*/{ "obj": { // This is a comment! "decimal": 0.0,