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

Constant_BoolValue never appears in has statement #128

Open
moxianfeng opened this issue Mar 22, 2022 · 2 comments
Open

Constant_BoolValue never appears in has statement #128

moxianfeng opened this issue Mar 22, 2022 · 2 comments
Labels

Comments

@moxianfeng
Copy link

When i try to use bool variable in has statement,
filter like bottom, and i always got Constant_StringValue type

a.c:true
a.c:True
a.c:TRUE

so, I try to strings.ParseBool to get bool variable,but how can'i check explicit string declaration between a.c:"true" and a.c:true

@jsocol
Copy link

jsocol commented Mar 31, 2023

There's a missing piece in the filtering.Declarations type, which doesn't include the constants true and false, or a way to declare those constants. There is a function NewConstantDeclaration(), which is used internally, but there's no public API to use the output as a declaration.

I had to add two new "ident" declarations to work around this:

// var parsedExp *expr.ParsedExpr

declOpts := []filtering.DeclarationOptions{
  filtering.DeclareStandardFunctions(),
  filtering.DeclareIdent("true", filtering.TypeBool),
  filtering.DeclareIdent("false", filtering.TypeBool),
}

decl := filtering.NewDeclarations(declOpts...)

checker := &filtering.Checker{}
checker.Init(parsedExp.Expr, parsedExp.SourceInfo, decl)

And then while walking over the parsed expression tree, where I pull out the constants like int64s, strings, etc, I do a check first, to see if it's an ident:

// var curr *expr.Expr
ident := curr.GetIdentExpr()
if ident != nil {
  if ident.Name == "true" {
    // treat as a `true` constant
  } else if ident.Name == "false" {
    // treat as a `false` constant
  }
}

constant := curr.GetConstExpr()
if constant != nil {
  switch kind := constant.Kind.(type) {
    // etc

It would be nice if true and false were part of the standard declarations, but I get why they aren't.

As an alternative, it would be great if filtering.Declarations.declare(*expr.Decl) were exported as an escape hatch for more advanced uses, or possibly a function like

func DeclareConstant(name string, t *expr.Type, value *expr.Constant) DeclarationOption {
  return func(d *Declarations) error {
    return d.declareConstant(name, t, value)
  }
}

Copy link

github-actions bot commented Apr 8, 2024

This issue has been open for 365 days with no activity. Marking as stale.

@github-actions github-actions bot added the Stale label Apr 8, 2024
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