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

Method AddStatement

pkg/dsl/ast/schema.go:50–111  ·  view source on GitHub ↗

AddStatement adds a new statement to an entity within the schema. It also updates the schema's references.

(entityName string, stmt Statement)

Source from the content-addressed store, hash-verified

48
49// AddStatement adds a new statement to an entity within the schema. It also updates the schema's references.
50func (sch *Schema) AddStatement(entityName string, stmt Statement) error {
51 // Loop through all statements in the schema to find the one matching the entity name.
52 var entityStmt *EntityStatement
53 for _, statement := range sch.Statements {
54 if statement.GetName() == entityName {
55 // Attempt to cast the found statement to an EntityStatement.
56 es, ok := statement.(*EntityStatement)
57 if ok {
58 entityStmt = es // Successfully found and cast the EntityStatement.
59 break
60 } else {
61 return errors.New(base.ErrorCode_ERROR_CODE_CANNOT_CONVERT_TO_ENTITY_STATEMENT.String()) // Casting failed.
62 }
63 }
64 }
65
66 // If no matching entity was found, return an error.
67 if entityStmt == nil {
68 return errors.New(base.ErrorCode_ERROR_CODE_ENTITY_STATEMENT_NOT_FOUND.String())
69 }
70
71 // Construct a unique key for the new statement.
72 refKey := fmt.Sprintf("%s#%s", entityName, stmt.GetName())
73
74 // Check if a reference for this statement already exists in the schema.
75 if sch.GetReferences().IsReferenceExist(refKey) {
76 return errors.New(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String()) // Avoid duplicating references.
77 }
78
79 // Add the new statement to the appropriate list in the EntityStatement, based on its type.
80 switch stmt.StatementType() {
81 case PERMISSION_STATEMENT:
82 // Append the new permission statement to the list of permission statements for the entity.
83 entityStmt.PermissionStatements = append(entityStmt.PermissionStatements, stmt)
84 // Add a new permission reference to the schema's references.
85 return sch.GetReferences().AddPermissionReference(refKey)
86
87 case RELATION_STATEMENT:
88 // Append the new relation statement to the list of relation statements for the entity.
89 entityStmt.RelationStatements = append(entityStmt.RelationStatements, stmt)
90 // Check if the statement can be cast to a RelationStatement and add its references.
91 if rs, ok := stmt.(*RelationStatement); ok {
92 return sch.GetReferences().AddRelationReferences(refKey, rs.RelationTypes)
93 } else {
94 return errors.New(base.ErrorCode_ERROR_CODE_CANNOT_CONVERT_TO_RELATION_STATEMENT.String())
95 }
96
97 case ATTRIBUTE_STATEMENT:
98 // Append the new attribute statement to the list of attribute statements for the entity.
99 entityStmt.AttributeStatements = append(entityStmt.AttributeStatements, stmt)
100 // Check if the statement can be cast to an AttributeStatement and add its references.
101 if as, ok := stmt.(*AttributeStatement); ok {
102 return sch.GetReferences().AddAttributeReferences(refKey, as.AttributeType)
103 } else {
104 return errors.New(base.ErrorCode_ERROR_CODE_CANNOT_CONVERT_TO_ATTRIBUTE_STATEMENT.String())
105 }
106
107 default:

Callers 2

parser_test.goFile · 0.80
PartialWriteMethod · 0.80

Calls 8

GetReferencesMethod · 0.95
IsReferenceExistMethod · 0.80
AddRelationReferencesMethod · 0.80
GetNameMethod · 0.65
StringMethod · 0.65
StatementTypeMethod · 0.65

Tested by

no test coverage detected