MCPcopy
hub / github.com/dgraph-io/dgraph / verifyTypes

Function verifyTypes

worker/mutation.go:798–860  ·  view source on GitHub ↗
(ctx context.Context, m *pb.Mutations)

Source from the content-addressed store, hash-verified

796}
797
798func verifyTypes(ctx context.Context, m *pb.Mutations) error {
799 // Create a set of all the predicates included in this schema request.
800 reqPredSet := make(map[string]struct{}, len(m.Schema))
801 for _, schemaUpdate := range m.Schema {
802 reqPredSet[schemaUpdate.Predicate] = struct{}{}
803 }
804
805 // Create a set of all the predicates already present in the schema.
806 var fields []string
807 for _, t := range m.Types {
808 if t.TypeName == "" {
809 return errors.Errorf("Type name must be specified in type update")
810 }
811
812 if err := typeSanityCheck(t); err != nil {
813 return err
814 }
815
816 for _, field := range t.Fields {
817 fieldName := field.Predicate
818 ns, attr := x.ParseNamespaceAttr(fieldName)
819 if attr[0] == '~' {
820 fieldName = x.NamespaceAttr(ns, attr[1:])
821 }
822
823 if _, ok := reqPredSet[fieldName]; !ok {
824 fields = append(fields, fieldName)
825 }
826 }
827 }
828
829 // Retrieve the schema for those predicates.
830 schemas, err := GetSchemaOverNetwork(ctx, &pb.SchemaRequest{Predicates: fields})
831 if err != nil {
832 return errors.Wrapf(err, "cannot retrieve predicate information")
833 }
834 schemaSet := make(map[string]struct{})
835 for _, schemaNode := range schemas {
836 schemaSet[schemaNode.Predicate] = struct{}{}
837 }
838
839 for _, t := range m.Types {
840 // Verify all the fields in the type are already on the schema or come included in
841 // this request.
842 for _, field := range t.Fields {
843 fieldName := field.Predicate
844 ns, attr := x.ParseNamespaceAttr(fieldName)
845 if attr[0] == '~' {
846 fieldName = x.NamespaceAttr(ns, attr[1:])
847 }
848
849 _, inSchema := schemaSet[fieldName]
850 _, inRequest := reqPredSet[fieldName]
851 if !inSchema && !inRequest {
852 return errors.Errorf(
853 "Schema does not contain a matching predicate for field %s in type %s",
854 field.Predicate, t.TypeName)
855 }

Callers 1

MutateOverNetworkFunction · 0.85

Calls 5

ParseNamespaceAttrFunction · 0.92
NamespaceAttrFunction · 0.92
typeSanityCheckFunction · 0.85
GetSchemaOverNetworkFunction · 0.85
ErrorfMethod · 0.45

Tested by

no test coverage detected