Skip to content

interface conversion: interface {} is nil, not string (1:9) #652

Closed
@mbardelmeijer

Description

@mbardelmeijer

When running the following expression, it returns in the error interface conversion: interface {} is nil, not string (1:9)

We would like to handle this gracefully, as the query parameter are optional that we're matching on. We prefer not to wrap it in string(), as our customer may create their own expression, and prefer to have this expression as clean as possible.

Is there any way to combat this, with for instance AllowUndefinedVariables? Or can expr-lang be modified to allow contains queries etc. on possible nil types?

env := map[string]interface{}{
	"query": map[string]interface{}{
		"another_query": "test",
	},
}

expression := "query.b contains 'test'"
prg, _ := expr.Compile(expression, expr.Env(env))
result, err := expr.Run(prg, env)
fmt.Println(result)
fmt.Println(err)

Output:

interface conversion: interface {} is nil, not string (1:9)
 | query.b contains 'test'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions