Compile compiles the schema into a list of entity definitions. Returns a slice of EntityDefinition pointers and an error, if any.
()
| 32 | // Compile compiles the schema into a list of entity definitions. |
| 33 | // Returns a slice of EntityDefinition pointers and an error, if any. |
| 34 | func (t *Compiler) Compile() ([]*base.EntityDefinition, []*base.RuleDefinition, error) { |
| 35 | // If withoutReferenceValidation is not set to true, validate the schema for reference errors. |
| 36 | if t.withReferenceValidation { |
| 37 | err := t.schema.Validate() |
| 38 | if err != nil { |
| 39 | return nil, nil, err |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Create an empty slice to hold the entity definitions. |
| 44 | entities := make([]*base.EntityDefinition, 0, len(t.schema.Statements)) |
| 45 | rules := make([]*base.RuleDefinition, 0, len(t.schema.Statements)) |
| 46 | |
| 47 | // Loop through each statement in the schema. |
| 48 | for _, statement := range t.schema.Statements { |
| 49 | switch v := statement.(type) { // Check statement type |
| 50 | case *ast.EntityStatement: |
| 51 | // Compile the EntityStatement into an EntityDefinition. |
| 52 | entityDef, err := t.compileEntity(v) // Compile entity |
| 53 | if err != nil { |
| 54 | return nil, nil, err |
| 55 | } |
| 56 | |
| 57 | // Append the EntityDefinition to the slice of entity definitions. |
| 58 | entities = append(entities, entityDef) |
| 59 | case *ast.RuleStatement: |
| 60 | // Compile the RuleStatement into a RuleDefinition. |
| 61 | ruleDef, err := t.compileRule(v) // Compile rule |
| 62 | if err != nil { |
| 63 | return nil, nil, err |
| 64 | } |
| 65 | |
| 66 | // Append the RuleDefinition to the slice of rule definitions. |
| 67 | rules = append(rules, ruleDef) |
| 68 | default: |
| 69 | return nil, nil, errors.New("invalid statement") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return entities, rules, nil |
| 74 | } |
| 75 | |
| 76 | // compile - compiles an EntityStatement into an EntityDefinition |
| 77 | func (t *Compiler) compileEntity(sc *ast.EntityStatement) (*base.EntityDefinition, error) { |