NewSchemaFromEntityAndRuleDefinitions creates a new base.SchemaDefinition from entity and rule definitions. It takes two slices of pointers to base.EntityDefinition and base.RuleDefinition, representing the entities and rules to include in the schema. It returns a pointer to the created base.SchemaD
(entities []*base.EntityDefinition, rules []*base.RuleDefinition)
| 28 | // It takes two slices of pointers to base.EntityDefinition and base.RuleDefinition, representing the entities and rules to include in the schema. |
| 29 | // It returns a pointer to the created base.SchemaDefinition. |
| 30 | func NewSchemaFromEntityAndRuleDefinitions(entities []*base.EntityDefinition, rules []*base.RuleDefinition) *base.SchemaDefinition { |
| 31 | // Create a new base.SchemaDefinition with empty maps for EntityDefinitions, RuleDefinitions, and References. |
| 32 | schema := &base.SchemaDefinition{ |
| 33 | EntityDefinitions: map[string]*base.EntityDefinition{}, |
| 34 | RuleDefinitions: map[string]*base.RuleDefinition{}, |
| 35 | References: map[string]base.SchemaDefinition_Reference{}, |
| 36 | } |
| 37 | |
| 38 | // Process each entity in the entities slice. |
| 39 | for _, entity := range entities { |
| 40 | // If the entity's Relations map is nil, initialize it as an empty map. |
| 41 | if entity.Relations == nil { |
| 42 | entity.Relations = map[string]*base.RelationDefinition{} |
| 43 | } |
| 44 | |
| 45 | // If the entity's Permissions map is nil, initialize it as an empty map. |
| 46 | if entity.Permissions == nil { |
| 47 | entity.Permissions = map[string]*base.PermissionDefinition{} |
| 48 | } |
| 49 | |
| 50 | // Add the entity to the EntityDefinitions map of the schema, using its name as the key. |
| 51 | schema.EntityDefinitions[entity.Name] = entity |
| 52 | |
| 53 | // Set a reference for the entity in the References map of the schema, indicating it's an entity reference. |
| 54 | schema.References[entity.Name] = base.SchemaDefinition_REFERENCE_ENTITY |
| 55 | } |
| 56 | |
| 57 | // Process each rule in the rules slice. |
| 58 | for _, rule := range rules { |
| 59 | // If the rule's Arguments map is nil, initialize it as an empty map. |
| 60 | if rule.Arguments == nil { |
| 61 | rule.Arguments = map[string]base.AttributeType{} |
| 62 | } |
| 63 | |
| 64 | // Add the rule to the RuleDefinitions map of the schema, using its name as the key. |
| 65 | schema.RuleDefinitions[rule.Name] = rule |
| 66 | |
| 67 | // Set a reference for the rule in the References map of the schema, indicating it's a rule reference. |
| 68 | schema.References[rule.Name] = base.SchemaDefinition_REFERENCE_RULE |
| 69 | } |
| 70 | |
| 71 | // Return the created schema. |
| 72 | return schema |
| 73 | } |
| 74 | |
| 75 | func NewEntityAndRuleDefinitionsFromStringDefinitions(validation bool, definitions ...string) ([]*base.EntityDefinition, []*base.RuleDefinition, error) { |
| 76 | // Use the parser to parse the string definitions into a Schema object |
no outgoing calls
no test coverage detected