Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithoutNormalize Compiler Option #274

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ type compiler struct {
builtinScope *scopeinfo
scopes []*scopeinfo
scopecnt int
skipNormalize bool
}

// Code is a compiled jq query.
type Code struct {
variables []string
codes []*code
codeinfos []codeinfo
variables []string
codes []*code
codeinfos []codeinfo
skipNormalize bool
}

// Run runs the code with the variable values (which should be in the
Expand All @@ -47,9 +49,16 @@ func (c *Code) RunWithContext(ctx context.Context, v any, values ...any) Iter {
return NewIter(&expectedVariableError{c.variables[len(values)]})
}
for i, v := range values {
values[i] = normalizeNumbers(v)
values[i] = c.normalize(v)
}
return newEnv(ctx).execute(c, c.normalize(v), values...)
}

func (c *Code) normalize(v any) any {
if c.skipNormalize {
return v
}
return newEnv(ctx).execute(c, normalizeNumbers(v), values...)
return Normalize(v)
}

type scopeinfo struct {
Expand Down Expand Up @@ -113,9 +122,10 @@ func Compile(q *Query, options ...CompilerOption) (*Code, error) {
c.optimizeTailRec()
c.optimizeCodeOps()
return &Code{
variables: c.variables,
codes: c.codes,
codeinfos: c.codeinfos,
variables: c.variables,
codes: c.codes,
codeinfos: c.codeinfos,
skipNormalize: c.skipNormalize,
}, nil
}

Expand All @@ -132,6 +142,14 @@ func (c *compiler) compile(q *Query) error {
return nil
}

func (c *compiler) normalize(v any) any {
if c.skipNormalize {
return v
}

return Normalize(v)
}

func (c *compiler) compileImport(i *Import) error {
var path, alias string
var err error
Expand Down Expand Up @@ -160,7 +178,7 @@ func (c *compiler) compileImport(i *Import) error {
} else {
return fmt.Errorf("module not found: %q", path)
}
vals = normalizeNumbers(vals)
vals = c.normalize(vals)
c.append(&code{op: oppush, v: vals})
c.append(&code{op: opstore, v: c.pushVariable(alias)})
c.append(&code{op: oppush, v: vals})
Expand Down Expand Up @@ -1218,7 +1236,7 @@ func (c *compiler) funcInput(any, []any) any {
if !ok {
return errors.New("break")
}
return normalizeNumbers(v)
return c.normalize(v)
}

func (c *compiler) funcModulemeta(v any, _ []any) any {
Expand Down
30 changes: 30 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,36 @@ func TestCodeRun_Race(t *testing.T) {
wg.Wait()
}

func TestCode_RunWithoutNormalize(t *testing.T) {
query, err := gojq.Parse(".foo")
if err != nil {
log.Fatalln(err)
}
code, err := gojq.Compile(query)
if err != nil {
log.Fatalln(err)
}
input := map[string]any{"foo": int32(1)}
_ = code.RunWithContext(context.Background(), input)
switch v := input["foo"].(type) {
case int: // ok
default:
t.Fatalf("expected int, got %T", v)
}

code, err = gojq.Compile(query, gojq.WithoutNormalize())
if err != nil {
log.Fatalln(err)
}
input = map[string]any{"foo": int32(1)} //need to recreate it as it was modified
_ = code.RunWithContext(context.Background(), input)
switch v := input["foo"].(type) {
case int32: // ok
default:
t.Fatalf("expected int32, got %T", v)
}
}

func BenchmarkCompile(b *testing.B) {
cnt, err := os.ReadFile("builtin.jq")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion func.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ func funcFromJSON(v any) any {
if _, err := dec.Token(); err != io.EOF {
return &func0TypeError{"fromjson", v}
}
return normalizeNumbers(w)
return Normalize(w)
}

func funcFormat(v, x any) any {
Expand Down
15 changes: 12 additions & 3 deletions normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ func normalizeNumber(v json.Number) any {
return math.Inf(1)
}

func normalizeNumbers(v any) any {
// Normalize any given data.
//
// This function will in particular change any given map[any]any
// so that all the integer values will be of type int and all the
// floating point values will be of type float64. This method will
// modify the given data in place.
//
// Typically running a gojq query will normalize the data automatically
// unless the [WithoutNormalize] option has been specified.
func Normalize(v any) any {
switch v := v.(type) {
case json.Number:
return normalizeNumber(v)
Expand Down Expand Up @@ -70,12 +79,12 @@ func normalizeNumbers(v any) any {
return float64(v)
case []any:
for i, x := range v {
v[i] = normalizeNumbers(x)
v[i] = Normalize(x)
}
return v
case map[string]any:
for k, x := range v {
v[k] = normalizeNumbers(x)
v[k] = Normalize(x)
}
return v
default:
Expand Down
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@ func WithInputIter(inputIter Iter) CompilerOption {
c.inputIter = inputIter
}
}

// WithoutNormalize ensures any given data won't be modified when executing code.
// Note that the input data is expected to be normalized using the [Normalize] function.
func WithoutNormalize() CompilerOption {
return func(c *compiler) {
c.skipNormalize = true
}
}