Skip to content

Enhancement Request: support extracting expected program inputs #374

@scosman

Description

@scosman

I'm building a system where there is a non trivial cost to building the environment and the program isn't known in advance. There are a few hundred possible inputs, but most programs will only need one or two. I'd like to only populate the environment with the necessary inputs, saving a lot of compute cost getting the rest.

Is there any good way (or can one be added) to get the expected inputs? Ideally a map of name:type so I can validate the types match.

I've hacked one together below, but I'm not certain it covers all cases (OpLoadField/OpFetchField might be missing?), and it doesn't give me the expected type. If this isn't an ER you'd want to take to main, can you comment on if I'm missing anything important in this version?

Thanks in advance!

	code := "(a > 5555) && b && 'asdf' == c && 2 in [d, 3, 4]"
	emptyEnv := expr.Env(map[string]interface{}{})
	program, err := expr.Compile(code, emptyEnv, expr.AllowUndefinedVariables(), expr.AsBool())

	variableMap := map[string]bool{}
	for i, bytecode := range program.Bytecode {
		if bytecode == vm.OpLoadFast {
			arg := program.Arguments[i]
			varName, ok := program.Constants[arg].(string)
			if ok {
				variableMap[varName] = true
			}
		}
	}
	variables := maps.Keys(variableMap)
        fmt.Printf("Program variables: %v\n", variables)
        // Program variables: [a b c d]

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