forked from dgraph-io/dgraph
/
proto.go
80 lines (65 loc) · 2.33 KB
/
proto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package query
import (
"encoding/base64"
"time"
"gopkg.in/adibiarsotp/dgraph.v83/protos"
"gopkg.in/adibiarsotp/dgraph.v83/types"
"gopkg.in/adibiarsotp/dgraph.v83/x"
geom "github.com/twpayne/go-geom"
)
// This file contains helper functions for converting scalar types to
// protobuf values.
func toProtoValue(v types.Val) *protos.Value {
switch v.Tid {
case types.StringID:
return &protos.Value{&protos.Value_StrVal{v.Value.(string)}}
case types.IntID:
return &protos.Value{&protos.Value_IntVal{v.Value.(int64)}}
case types.FloatID:
return &protos.Value{&protos.Value_DoubleVal{v.Value.(float64)}}
case types.BoolID:
return &protos.Value{&protos.Value_BoolVal{v.Value.(bool)}}
case types.DateTimeID:
val := v.Value.(time.Time)
return &protos.Value{&protos.Value_StrVal{val.Format(time.RFC3339)}}
case types.BinaryID:
val := v.Value.([]byte)
dst := make([]byte, base64.StdEncoding.DecodedLen(len(val)))
n, _ := base64.StdEncoding.Decode(dst, val)
if n < len(dst) {
dst = dst[:n]
}
return &protos.Value{&protos.Value_BytesVal{dst}}
case types.GeoID:
b := types.ValueForType(types.BinaryID)
src := types.ValueForType(types.GeoID)
src.Value = v.Value.(geom.T)
x.Check(types.Marshal(src, &b))
return &protos.Value{&protos.Value_GeoVal{b.Value.([]byte)}}
case types.PasswordID:
return &protos.Value{&protos.Value_PasswordVal{v.Value.(string)}}
case types.UidID:
return &protos.Value{&protos.Value_UidVal{v.Value.(uint64)}}
case types.DefaultID:
return &protos.Value{&protos.Value_DefaultVal{v.Value.(string)}}
default:
// A type that isn't supported in the proto
return nil
}
}