MCPcopy Create free account
hub / github.com/evilsocket/sum / validate

Function validate

service/compiler.go:19–45  ·  view source on GitHub ↗
(oracle *pb.Oracle)

Source from the content-addressed store, hash-verified

17)
18
19func validate(oracle *pb.Oracle) (call string, args []string, err error) {
20 var prototype *ast.FunctionDeclaration
21 // first try to parse the oracle and validate that
22 // it starts with a function declaration
23 program, err := parser.ParseFile(nil, "", oracle.Code, 0)
24 ok := true
25 if err != nil {
26 return "", nil, err
27 } else if program.DeclarationList == nil || len(program.DeclarationList) < 1 {
28 return "", nil, errNoDeclarations
29 } else if prototype, ok = program.DeclarationList[0].(*ast.FunctionDeclaration); !ok {
30 return "", nil, fmt.Errorf("expected function declaration, found %T", program.DeclarationList[0])
31 }
32
33 // use the function declaration in order to build the function call
34 args = []string{}
35 if prototype.Function.ParameterList != nil && prototype.Function.ParameterList.List != nil {
36 args = make([]string, len(prototype.Function.ParameterList.List))
37 for i, param := range prototype.Function.ParameterList.List {
38 args[i] = param.Name
39 }
40 }
41 call = fmt.Sprintf("%s(%s)",
42 prototype.Function.Name.Name,
43 strings.Join(args, ","))
44 return
45}
46
47// Compiles a raw oracle.
48func compile(oracle *pb.Oracle) (*compiled, error) {

Callers 1

compileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected