Skip to content

Commit

Permalink
Fix small issues
Browse files Browse the repository at this point in the history
  • Loading branch information
stirante committed Oct 4, 2024
1 parent f925ab3 commit 2a7348b
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 26 deletions.
8 changes: 5 additions & 3 deletions jsonte/expression_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,11 @@ func (v *ExpressionVisitor) VisitField(context *parser.FieldContext) (types.Json
}
}
if methodName == nil || !functions.HasFunction(*methodName) {
find := functions.FindMisspelling(*methodName)
if find != nil {
return types.Null, burrito.WrappedErrorf("Function '%s' not found, did you mean '%s'?", *methodName, *find)
if methodName != nil {
find := functions.FindMisspelling(*methodName)
if find != nil {
return types.Null, burrito.WrappedErrorf("Function '%s' not found, did you mean '%s'?", *methodName, *find)
}
}
return types.Null, burrito.WrappedErrorf("Function '%s' not found!", context.Field(0).GetText())
}
Expand Down
4 changes: 2 additions & 2 deletions jsonte/functions/file_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/MCDevKit/jsonte/jsonte/types"
"github.com/MCDevKit/jsonte/jsonte/utils"
"github.com/gobwas/glob"
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -283,7 +283,7 @@ func load(s *types.JsonString) (*types.JsonObject, error) {
if err != nil {
return types.NewJsonObject(), burrito.WrapErrorf(err, "Failed to resolve path %s", s.StringValue())
}
readAll, err := ioutil.ReadAll(resolver)
readAll, err := io.ReadAll(resolver)
if err != nil {
return types.NewJsonObject(), burrito.WrapErrorf(err, "Failed to read file %s", s.StringValue())
}
Expand Down
4 changes: 2 additions & 2 deletions jsonte/functions/minecraft_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,15 +832,15 @@ func findPackVersions(isBp bool, uuid string, version *types.Semver) (utils.Navi
if ver.Major != version.Major {
continue
}
if ver.Minor != closest.Minor {
if closest != nil && ver.Minor != closest.Minor {
continue
}
if ver.Patch == version.Patch {
closest = ver
release = vs1.Get(tag)
break
}
if math.Abs(float64(ver.Patch-version.Patch)) < math.Abs(float64(closest.Patch-version.Patch)) {
if closest == nil || math.Abs(float64(ver.Patch-version.Patch)) < math.Abs(float64(closest.Patch-version.Patch)) {
closest = ver
release = vs1.Get(tag)
}
Expand Down
6 changes: 3 additions & 3 deletions jsonte/json_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/MCDevKit/jsonte/jsonte/types"
"github.com/MCDevKit/jsonte/jsonte/utils"
"github.com/gammazero/deque"
"io/ioutil"
"io"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -357,7 +357,7 @@ func processCopy(c types.JsonType, visitor TemplateVisitor, modules map[string]J
if err != nil {
return types.NewJsonObject(), utils.WrapJsonErrorf(loopPath, err, "Failed to open %s", copyPath.StringValue())
}
all, err := ioutil.ReadAll(resolve)
all, err := io.ReadAll(resolve)
if err != nil {
return types.NewJsonObject(), utils.WrapJsonErrorf(loopPath, err, "Failed to read %s", copyPath.StringValue())
}
Expand Down Expand Up @@ -753,7 +753,7 @@ func (v *TemplateVisitor) visitString(str string, path string) (types.JsonType,
return nil, burrito.WrapErrorf(err, "Error evaluating '%s'", match.EscapedMatch)
}
if result.Value == nil {
return nil, utils.WrappedJsonErrorf(path, "The expression '%s' evaluated to null", match)
return nil, utils.WrappedJsonErrorf(path, "The expression '%s' evaluated to null", match.EscapedMatch)
}
if result.Action == types.Literal {
return result.Value, nil
Expand Down
2 changes: 1 addition & 1 deletion jsonte/safeio/io_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type IOResolver struct {
}

// Resolver Current Resolver used by jsonte to access IO
var Resolver IOResolver = DefaultIOResolver
var Resolver = DefaultIOResolver

// DefaultIOResolver Default IO Resolver, that uses the os package
var DefaultIOResolver = IOResolver{
Expand Down
10 changes: 4 additions & 6 deletions jsonte/types/json_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,12 @@ func (t *JsonPath) Get(x JsonType) (JsonType, error) {
return nil, burrito.WrappedErrorf("Cannot get %s from %s", t.StringValue(), x.StringValue())
}
}
var err error
for i := 0; i < len(t.Path); i++ {
//parent := x
x, err = x.Index(t.Path[i])
//x.UpdateParent(parent, t.Path[i])
y, err := x.Index(t.Path[i])
if err != nil {
return nil, burrito.WrapErrorf(err, "Cannot get %s from %s", t.StringValue(), x.StringValue())
}
x = y
}
return x, nil
}
Expand All @@ -156,12 +154,12 @@ func (t *JsonPath) Set(x, value JsonType) (JsonType, error) {
return original, burrito.WrappedErrorf("Cannot set %s in %s", t.StringValue(), x.StringValue())
}
}
var err error
for i := 0; i < len(t.Path)-1; i++ {
x, err = x.Index(t.Path[i])
y, err := x.Index(t.Path[i])
if err != nil {
return original, burrito.WrapErrorf(err, "Cannot get %s from %s", t.StringValue(), x.StringValue())
}
x = y
}
if b, ok := x.(*JsonObject); ok {
if k, ok := t.Path[len(t.Path)-1].(*JsonString); ok {
Expand Down
4 changes: 0 additions & 4 deletions jsonte/types/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/MCDevKit/jsonte/jsonte/utils"
"github.com/gammazero/deque"
"reflect"
"regexp"
"strings"
)

Expand Down Expand Up @@ -307,9 +306,6 @@ func IsEqualObject(a, b utils.NavigableMap[string, JsonType]) bool {
return true
}

// TODO: This should be moved to a shared package.
var actionPattern, _ = regexp.Compile("^\\{\\{(?:\\\\.|[^{}])+}}$")

// MergeObject merges two JSON objects into a new JSON object.
// If the same value, that is not an object or an array exists in both objects, the value from the second object will be used.
func MergeObject(template, parent *JsonObject, keepOverrides bool, path string) *JsonObject {
Expand Down
3 changes: 2 additions & 1 deletion test/eval_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"errors"
"fmt"
"github.com/Bedrock-OSS/go-burrito/burrito"
"github.com/MCDevKit/jsonte/jsonte"
Expand Down Expand Up @@ -662,7 +663,7 @@ func TestLambdaInfo(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
vars, args, err := jsonte.ParseLambda(tc.input)

if err != tc.expectedError {
if !errors.Is(err, tc.expectedError) {
t.Fatalf("Test case %d (%s) - Expected error: %v, got: %v", idx, tc.name, tc.expectedError, err)
}

Expand Down
8 changes: 4 additions & 4 deletions test/safeio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package test

import (
"github.com/MCDevKit/jsonte/jsonte/safeio"
"io/ioutil"
"io"
"path/filepath"
"sort"
"testing"
Expand All @@ -17,7 +17,7 @@ func TestFakeOpen(t *testing.T) {
if err != nil {
t.Fatal(err)
}
all, err := ioutil.ReadAll(open)
all, err := io.ReadAll(open)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestFakeCreate(t *testing.T) {
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(in)
data, err := io.ReadAll(in)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestFakeCreate2(t *testing.T) {
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(in)
data, err := io.ReadAll(in)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 2a7348b

Please sign in to comment.