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

Function ObjectValue

types/conversion.go:637–713  ·  view source on GitHub ↗

ObjectValue converts into api.Value.

(id TypeID, value interface{})

Source from the content-addressed store, hash-verified

635
636// ObjectValue converts into api.Value.
637func ObjectValue(id TypeID, value interface{}) (*api.Value, error) {
638 def := &api.Value{Val: &api.Value_StrVal{StrVal: ""}}
639 var ok bool
640 // Lets set the object value according to the storage type.
641 switch id {
642 case StringID:
643 var v string
644 if v, ok = value.(string); !ok {
645 return def, errors.Errorf("Expected value of type string. Got : %v", value)
646 }
647 return &api.Value{Val: &api.Value_StrVal{StrVal: v}}, nil
648 case DefaultID:
649 var v string
650 if v, ok = value.(string); !ok {
651 return def, errors.Errorf("Expected value of type string. Got : %v", value)
652 }
653 return &api.Value{Val: &api.Value_DefaultVal{DefaultVal: v}}, nil
654 case IntID:
655 var v int64
656 if v, ok = value.(int64); !ok {
657 return def, errors.Errorf("Expected value of type int64. Got : %v", value)
658 }
659 return &api.Value{Val: &api.Value_IntVal{IntVal: v}}, nil
660 case FloatID:
661 var v float64
662 if v, ok = value.(float64); !ok {
663 return def, errors.Errorf("Expected value of type float64. Got : %v", value)
664 }
665 return &api.Value{Val: &api.Value_DoubleVal{DoubleVal: v}}, nil
666 case BoolID:
667 var v bool
668 if v, ok = value.(bool); !ok {
669 return def, errors.Errorf("Expected value of type bool. Got : %v", value)
670 }
671 return &api.Value{Val: &api.Value_BoolVal{BoolVal: v}}, nil
672 case BinaryID:
673 var v []byte
674 if v, ok = value.([]byte); !ok {
675 return def, errors.Errorf("Expected value of type []byte. Got : %v", value)
676 }
677 return &api.Value{Val: &api.Value_BytesVal{BytesVal: v}}, nil
678 // Geo, datetime, and BigFloat are stored in binary format in the N-Quad, so lets
679 // convert them here.
680 case BigFloatID:
681 b, err := toBinary(id, value)
682 if err != nil {
683 return def, err
684 }
685 return &api.Value{Val: &api.Value_BigfloatVal{BigfloatVal: b}}, nil
686 case GeoID:
687 b, err := toBinary(id, value)
688 if err != nil {
689 return def, err
690 }
691 return &api.Value{Val: &api.Value_GeoVal{GeoVal: b}}, nil
692 case DateTimeID:
693 b, err := toBinary(id, value)
694 if err != nil {

Callers 4

updateValInNQuadsFunction · 0.92
ParseRDFFunction · 0.92
tryParseAsGeoFunction · 0.92

Calls 2

toBinaryFunction · 0.85
ErrorfMethod · 0.45