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

Function ValidateAndConvert

worker/mutation.go:508–597  ·  view source on GitHub ↗

ValidateAndConvert checks compatibility or converts to the schema type if the storage type is specified. If no storage type is specified then it converts to the schema type.

(edge *pb.DirectedEdge, su *pb.SchemaUpdate)

Source from the content-addressed store, hash-verified

506// ValidateAndConvert checks compatibility or converts to the schema type if the storage type is
507// specified. If no storage type is specified then it converts to the schema type.
508func ValidateAndConvert(edge *pb.DirectedEdge, su *pb.SchemaUpdate) error {
509
510 if isDeletePredicateEdge(edge) {
511 return nil
512 }
513 if types.TypeID(edge.ValueType) == types.DefaultID && isStarAll(edge.Value) {
514 return nil
515 }
516
517 if strings.Contains(su.Predicate, hnsw.VecKeyword) {
518 return errors.Errorf("Not allowed to insert mutations in vector index keys, edge: [%v]", edge)
519 }
520
521 storageType := posting.TypeID(edge)
522 schemaType := types.TypeID(su.ValueType)
523
524 // type checks
525 switch {
526 case edge.Lang != "" && !su.GetLang():
527 return errors.Errorf("Attr: [%v] should have @lang directive in schema to mutate edge: [%v]",
528 x.ParseAttr(edge.Attr), edge)
529
530 case !schemaType.IsScalar() && !storageType.IsScalar():
531 return nil
532
533 case !schemaType.IsScalar() && storageType.IsScalar():
534 return errors.Errorf("Input for predicate %q of type uid is scalar. Edge: %v",
535 x.ParseAttr(edge.Attr), edge)
536
537 case schemaType.IsScalar() && !storageType.IsScalar():
538 return errors.Errorf("Input for predicate %q of type scalar is uid. Edge: %v",
539 x.ParseAttr(edge.Attr), edge)
540
541 case schemaType == types.TypeID(pb.Posting_VFLOAT):
542 if !(storageType == types.TypeID(pb.Posting_VFLOAT) || storageType == types.TypeID(pb.Posting_STRING) ||
543 storageType == types.TypeID(pb.Posting_DEFAULT)) {
544 return errors.Errorf("Input for predicate %q of type vector is not vector."+
545 " Did you forget to add quotes before []?. Edge: %v", x.ParseAttr(edge.Attr), edge)
546 }
547
548 // The suggested storage type matches the schema, OK! (Nothing to do ...)
549 case storageType == schemaType && schemaType != types.DefaultID:
550 return nil
551
552 // We accept the storage type iff we don't have a schema type and a storage type is specified.
553 case schemaType == types.DefaultID:
554 schemaType = storageType
555 }
556
557 var (
558 dst types.Val
559 err error
560 )
561
562 src := types.Val{Tid: types.TypeID(edge.ValueType), Value: edge.Value}
563 // check compatibility of schema type and storage type
564 // The goal is to convert value on edge to value type defined by schema.
565 if dst, err = types.Convert(src, schemaType); err != nil {

Callers 4

TestConvertEdgeTypeFunction · 0.85
proposeAndWaitMethod · 0.85
runMutationFunction · 0.85

Calls 13

TypeIDTypeAlias · 0.92
TypeIDFunction · 0.92
ParseAttrFunction · 0.92
ConvertFunction · 0.92
ValueForTypeFunction · 0.92
MarshalFunction · 0.92
isDeletePredicateEdgeFunction · 0.85
isStarAllFunction · 0.85
IsScalarMethod · 0.80
ErrorfMethod · 0.45
GetLangMethod · 0.45
GetAttrMethod · 0.45

Tested by 2

TestConvertEdgeTypeFunction · 0.68