github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/namespace/metadata.go (about) 1 package namespace 2 3 import ( 4 "google.golang.org/protobuf/types/known/anypb" 5 6 core "github.com/authzed/spicedb/pkg/proto/core/v1" 7 8 iv1 "github.com/authzed/spicedb/pkg/proto/impl/v1" 9 ) 10 11 // WithSourcePosition defines an interface for proto messages in core with SourcePosition information attached. 12 type WithSourcePosition interface { 13 GetSourcePosition() *core.SourcePosition 14 } 15 16 // userDefinedMetadataTypeUrls are the type URLs of any user-defined metadata found 17 // in the namespace proto. If placed here, FilterUserDefinedMetadataInPlace will remove the 18 // metadata when called on the namespace. 19 var userDefinedMetadataTypeUrls = map[string]struct{}{ 20 "type.googleapis.com/impl.v1.DocComment": {}, 21 } 22 23 // FilterUserDefinedMetadataInPlace removes user-defined metadata (e.g. comments) from the given namespace 24 // *in place*. 25 func FilterUserDefinedMetadataInPlace(nsconfig *core.NamespaceDefinition) { 26 nsconfig.Metadata = nil 27 for _, relation := range nsconfig.Relation { 28 if relation.Metadata != nil { 29 var filteredMessages []*anypb.Any 30 for _, msg := range relation.Metadata.MetadataMessage { 31 if _, ok := userDefinedMetadataTypeUrls[msg.TypeUrl]; !ok { 32 filteredMessages = append(filteredMessages, msg) 33 } 34 } 35 relation.Metadata.MetadataMessage = filteredMessages 36 } 37 } 38 } 39 40 // GetComments returns the comment metadata found within the given metadata message. 41 func GetComments(metadata *core.Metadata) []string { 42 if metadata == nil { 43 return []string{} 44 } 45 46 comments := []string{} 47 for _, msg := range metadata.MetadataMessage { 48 var dc iv1.DocComment 49 if err := msg.UnmarshalTo(&dc); err == nil { 50 comments = append(comments, dc.Comment) 51 } 52 } 53 54 return comments 55 } 56 57 // AddComment adds a comment to the given metadata message. 58 func AddComment(metadata *core.Metadata, comment string) (*core.Metadata, error) { 59 if metadata == nil { 60 metadata = &core.Metadata{} 61 } 62 63 var dc iv1.DocComment 64 dc.Comment = comment 65 66 encoded, err := anypb.New(&dc) 67 if err != nil { 68 return metadata, err 69 } 70 71 metadata.MetadataMessage = append(metadata.MetadataMessage, encoded) 72 return metadata, nil 73 } 74 75 // GetRelationKind returns the kind of the relation. 76 func GetRelationKind(relation *core.Relation) iv1.RelationMetadata_RelationKind { 77 metadata := relation.Metadata 78 if metadata == nil { 79 return iv1.RelationMetadata_UNKNOWN_KIND 80 } 81 82 for _, msg := range metadata.MetadataMessage { 83 var rm iv1.RelationMetadata 84 if err := msg.UnmarshalTo(&rm); err == nil { 85 return rm.Kind 86 } 87 } 88 89 return iv1.RelationMetadata_UNKNOWN_KIND 90 } 91 92 // SetRelationKind sets the kind of relation. 93 func SetRelationKind(relation *core.Relation, kind iv1.RelationMetadata_RelationKind) error { 94 metadata := relation.Metadata 95 if metadata == nil { 96 metadata = &core.Metadata{} 97 relation.Metadata = metadata 98 } 99 100 var rm iv1.RelationMetadata 101 rm.Kind = kind 102 103 encoded, err := anypb.New(&rm) 104 if err != nil { 105 return err 106 } 107 108 metadata.MetadataMessage = append(metadata.MetadataMessage, encoded) 109 return nil 110 }