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

Function valToBytes

query/outputnode.go:641–696  ·  view source on GitHub ↗
(v types.Val)

Source from the content-addressed store, hash-verified

639}
640
641func valToBytes(v types.Val) ([]byte, error) {
642 switch v.Tid {
643 case types.StringID, types.DefaultID:
644 switch str := v.Value.(type) {
645 case string:
646 return stringJsonMarshal(str), nil
647 default:
648 return json.Marshal(str)
649 }
650 case types.BinaryID:
651 return []byte(fmt.Sprintf("%q", v.Value)), nil
652 case types.IntID:
653 // In types.Convert(), we always convert to int64 for IntID type. fmt.Sprintf is slow
654 // and hence we are using strconv.FormatInt() here. Since int64 and int are most common int
655 // types we are using FormatInt for those.
656 switch num := v.Value.(type) {
657 case int64:
658 return []byte(strconv.FormatInt(num, 10)), nil
659 case int:
660 return []byte(strconv.FormatInt(int64(num), 10)), nil
661 default:
662 return []byte(fmt.Sprintf("%d", v.Value)), nil
663 }
664 case types.FloatID:
665 f, fOk := v.Value.(float64)
666
667 // +Inf, -Inf and NaN are not representable in JSON.
668 // Please see https://golang.org/src/encoding/json/encode.go?s=6458:6501#L573
669 if !fOk || math.IsInf(f, 0) || math.IsNaN(f) {
670 return nil, errors.New("Unsupported floating point number in float field")
671 }
672
673 return []byte(fmt.Sprintf("%g", f)), nil
674 case types.BoolID:
675 if v.Value.(bool) {
676 return boolTrue, nil
677 }
678 return boolFalse, nil
679 case types.DateTimeID:
680 t := v.Value.(time.Time)
681 return marshalTimeJson(t)
682 case types.GeoID:
683 return geojson.Marshal(v.Value.(geom.T))
684 case types.BigFloatID:
685 b := v.Value.(big.Float)
686 return b.MarshalText()
687 case types.UidID:
688 return []byte(fmt.Sprintf("\"%#x\"", v.Value)), nil
689 case types.PasswordID:
690 return []byte(fmt.Sprintf("%q", v.Value.(string))), nil
691 case types.VFloatID:
692 return json.Marshal(v.Value.([]float32))
693 default:
694 return nil, errors.New("Unsupported types.Val.Tid")
695 }
696}
697
698// marshalTimeJson does what time.MarshalJson does along with supporting RFC3339 non compliant

Callers 4

AddListValueMethod · 0.85
buildTestTreeFunction · 0.85
TestMarshalFloatFunction · 0.85
getObjectValFunction · 0.85

Calls 2

stringJsonMarshalFunction · 0.85
marshalTimeJsonFunction · 0.85

Tested by 2

buildTestTreeFunction · 0.68
TestMarshalFloatFunction · 0.68