MCPcopy Create free account
hub / github.com/Permify/permify / Compile

Method Compile

pkg/dsl/compiler/compiler.go:34–74  ·  view source on GitHub ↗

Compile compiles the schema into a list of entity definitions. Returns a slice of EntityDefinition pointers and an error, if any.

()

Source from the content-addressed store, hash-verified

32// Compile compiles the schema into a list of entity definitions.
33// Returns a slice of EntityDefinition pointers and an error, if any.
34func (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
77func (t *Compiler) compileEntity(sc *ast.EntityStatement) (*base.EntityDefinition, error) {

Callers 15

RunWithShapeMethod · 0.80
parseAndCompileSchemaFunction · 0.80
compiler_test.goFile · 0.80
compileRuleMethod · 0.80
validateFunction · 0.80
RuleFunction · 0.80
newSchemaFunction · 0.80
WriteMethod · 0.80
PartialWriteMethod · 0.80
walker_test.goFile · 0.80
schema_test.goFile · 0.80

Calls 3

compileEntityMethod · 0.95
compileRuleMethod · 0.95
ValidateMethod · 0.45

Tested by 3

newSchemaFunction · 0.64
newSchemaFunction · 0.64
newSchemaFunction · 0.64