Skip to content

Can not express map[string] interface property #324

@scorpinxia

Description

@scorpinxia

I have run some test below TEST1,I want the error happened( variables have no cpu1 struct),but It just return false

Also I do TEST2 like below , just change field 'cpu1' to 'request1' , It works (it return err , can not fetch cpu from nil )

again I do TEST3 like below , just change '==' to '>' , It works (it return err invalid operation: < float64 (1:27))

any ideas?

  1. TEST1

       `{
     	name:      "number expr",
     	matchExpr: "test.hello.request.cpu1 == 0.15",
     	variables: map[string]interface{}{
     		"test": map[string]interface{}{
     			"hello": map[string]interface{}{
     				"request": map[string]interface{}{
     					"cpu": 0.2,
     				},
     			},
     		},
     	},
     },`
    
  2. TEST2

       `{
     	name:      "number expr",
     	matchExpr: "test.hello.request1.cpu == 0.15",
     	variables: map[string]interface{}{
     		"test": map[string]interface{}{
     			"hello": map[string]interface{}{
     				"request": map[string]interface{}{
     					"cpu": 0.2,
     				},
     			},
     		},
     	},
     },`
    
  3. TEST3

       `{
     	name:      "number expr",
     	matchExpr: "test.hello.request.cpu1 < 0.15",
     	variables: map[string]interface{}{
     		"test": map[string]interface{}{
     			"hello": map[string]interface{}{
     				"request": map[string]interface{}{
     					"cpu": 0.2,
     				},
     			},
     		},
     	},
     },`
    
  4. Eval expression code

func MatchExprEval(matchExpr string, variables map[string]interface{}) (bool, error) {
	program, err := expr.Compile(matchExpr, expr.Env(variables), expr.AsBool())
	if err != nil {
		return false, err
	}
	output, err := expr.Run(program, variables)
	if err != nil {
		return false, err
	}

	var outputBool, ok bool
	if outputBool, ok = output.(bool); !ok {
		return false, fmt.Errorf("not a bool result")
	}
	return outputBool, nil
}

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