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

The type should be the field's when struct is nil #570

Closed
BubbleGarten opened this issue Feb 19, 2024 · 2 comments
Closed

The type should be the field's when struct is nil #570

BubbleGarten opened this issue Feb 19, 2024 · 2 comments
Labels

Comments

@BubbleGarten
Copy link

type Student struct {
	Name string
}

func GetStudent() *Student {
	return nil
}

func TestGetStudentName(t *testing.T) {
	eval, err := expr.Eval("student?.Name", map[string]interface{}{
		"student": GetStudent(),
	})
	if err != nil {
		t.Fatalf("expr.Eval failed: %v", err)
	}
	t.Logf("eval type: %v", reflect.TypeOf(eval)) // output is `eval type: *golang.Student`
}

Actually , I think it's because return nil directly when the struct is nil , but it's not so reasonable.
Is it better to return the zero value( empty string in this case) of the corresponding field ?

@antonmedv antonmedv added the bug label Feb 19, 2024
@antonmedv
Copy link
Member

Yes, you are right! Return should be just nil. I will fix it.

@antonmedv
Copy link
Member

Fixed. Now a proper nil is returned!

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

No branches or pull requests

2 participants