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

Can we make the fromJSON function support string pointer? #739

Closed
Cassius0924 opened this issue Dec 13, 2024 · 4 comments
Closed

Can we make the fromJSON function support string pointer? #739

Cassius0924 opened this issue Dec 13, 2024 · 4 comments
Labels

Comments

@Cassius0924
Copy link

The current fromJSON function only supports parameters of type string. If I pass a parameter of type *string, it throws an error.

@antonmedv antonmedv added the bug label Dec 13, 2024
@antonmedv
Copy link
Member

Looks like a deref is missing. Could you please share the whole example?

@Cassius0924
Copy link
Author

Cassius0924 commented Dec 13, 2024

Looks like a deref is missing. Could you please share the whole example?看起来缺少 deref。您能分享整个示例吗?

sure!

package main

import (
	"fmt"

	"github.com/expr-lang/expr"
)

func main() {
	jsonString := `{"Num": 1}`
	env := map[string]any{
		"aJSONString": &jsonString,
	}

	code := "fromJSON(aJSONString)"
	result, err := expr.Eval(code, env)
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}
panic: interface conversion: interface {} is *string, not string (1:1)
	 | fromJSON(aJSONString)
	 | ^

goroutine 1 [running]:

@Cassius0924
Copy link
Author

and split

@antonmedv
Copy link
Member

I fixed it! It took me quite a lot of programming, but I figure out the correct way to add support for *type for all builtins.

Compiler will add OpDeref only if it is needed. I also added a lot of tests for all builtins, so it should be fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants