-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgo_marshal.tmpl
23 lines (22 loc) · 1.23 KB
/
go_marshal.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func Marshal(prefix []byte, <<- param .Name >> S) []byte {
var enc = prefix
<< range .Fields ->>
<< if and (isMessage .) (not (isRepeatedType .)) ->>
enc = << goModuleName . >>.Marshal(enc, << param $.Name ->>.<< goName .Name >>)
<< else if isGoType . "enum" ->>
enc = marshal.WriteInt32(enc, uint32(<< param $.Name >>.<< goName .Name >>))
<< else if isGoType . "string" ->>
<< lower .Name>>Bytes := []byte(<<- param $.Name ->>.<< goName .Name >>)
enc = marshal.WriteInt(enc, uint64(len(<< lower .Name >>Bytes)))
enc = marshal.WriteBytes(enc, << lower .Name >>Bytes)
<< else if (and (isGoType . "byte") (isSliceType .)) ->>
enc = marshal.WriteInt(enc, uint64(len(<< param $.Name >>.<< goName .Name >>)))
enc = marshal.WriteBytes(enc, << param $.Name >>.<< goName .Name >>)
<< else if isSliceType . >>
enc = marshal.WriteInt(enc, uint64(len(<< param $.Name >>.<< goName .Name >>)))
enc = marshal.WriteSlice(enc, << param $.Name >>.<< goName .Name >>, << if isMessage . >><< goModuleName . >>.Marshal<< else >>marshal.Write<< marshalType . >><< end >>)
<< else ->>
enc = marshal.Write<<- marshalType . ->>(enc, << param $.Name ->>.<< goName .Name >>)
<< end ->><<- end >>
return enc
}