Replies: 2 comments 6 replies
-
The variable EnumVal is available in Expr. But list of constants not. And it’s not possible to get such list by reflection. Only by ast parsing. One possible solution is to add list of enum values as another variable/variables. |
Beta Was this translation helpful? Give feedback.
6 replies
-
Here's a working patcher solution for anyone else's reference: type viewifPatcher struct{}
var stringer = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
func (p *viewifPatcher) Visit(node *ast.Node) {
switch x := (*node).(type) {
case *ast.BinaryNode:
if lid, ok := x.Left.(*ast.IdentifierNode); ok && x.Operator == "==" {
if rid, ok := x.Right.(*ast.IdentifierNode); ok {
lt := lid.Type()
if lt == nil {
return
}
if lt.Implements(stringer) {
ast.Patch(node, &ast.BinaryNode{
Operator: "==",
Left: &ast.CallNode{
Callee: &ast.MemberNode{
Node: x.Left,
Property: &ast.StringNode{Value: "String"},
},
},
Right: &ast.StringNode{
Value: rid.Value,
},
})
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would like to be able to eval an expr like this:
EnumVal == Enum1
where I've defined ConstVal to be a standard Go const enum type:so the string values are available and could be tested against.
Any chance of adding this?
Beta Was this translation helpful? Give feedback.
All reactions