This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
/
Profile.pr.go
121 lines (104 loc) · 3.21 KB
/
Profile.pr.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014-2015 The Dename Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package proto
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/maditya/protobuf/proto"
)
type EncodedProfile struct {
Profile
Encoding []byte
}
func (m *EncodedProfile) UpdateEncoding() {
m.Encoding = MustMarshal(&m.Profile)
}
func (m *EncodedProfile) Reset() {
*m = EncodedProfile{}
}
func (m *EncodedProfile) Size() int {
return len(m.Encoding)
}
func (m *EncodedProfile) Marshal() ([]byte, error) {
size := m.Size()
data := make([]byte, size)
n, err := m.MarshalTo(data)
if err != nil {
return nil, err
}
return data[:n], nil
}
func (m *EncodedProfile) MarshalTo(data []byte) (int, error) {
return copy(data, m.Encoding), nil
}
func (m *EncodedProfile) Unmarshal(data []byte) error {
m.Encoding = append([]byte{}, data...)
return proto.Unmarshal(data, &m.Profile)
}
func NewPopulatedEncodedProfile(r randyClient, easy bool) *EncodedProfile {
this := &EncodedProfile{Profile: *NewPopulatedProfile(r, easy)}
this.UpdateEncoding()
return this
}
func (this *EncodedProfile) VerboseEqual(that interface{}) error {
if thatP, ok := that.(*EncodedProfile); ok {
return this.Profile.VerboseEqual(&thatP.Profile)
}
if thatP, ok := that.(EncodedProfile); ok {
return this.Profile.VerboseEqual(&thatP.Profile)
}
return fmt.Errorf("types don't match: %T != EncodedProfile", that)
}
func (this *EncodedProfile) Equal(that interface{}) bool {
if thatP, ok := that.(*EncodedProfile); ok {
return this.Profile.Equal(&thatP.Profile)
}
if thatP, ok := that.(EncodedProfile); ok {
return this.Profile.Equal(&thatP.Profile)
}
return false
}
func (this *EncodedProfile) GoString() string {
if this == nil {
return "nil"
}
return `proto.EncodedProfile{Profile: ` + this.Profile.GoString() + `, Encoding: ` + fmt.Sprintf("%#v", this.Encoding) + `}`
}
func (this *EncodedProfile) String() string {
if this == nil {
return "nil"
}
return `proto.EncodedProfile{Profile: ` + this.Profile.String() + `, Encoding: ` + fmt.Sprintf("%v", this.Encoding) + `}`
}
func (m *EncodedProfile) MarshalJSON() ([]byte, error) {
ret := make([]byte, base64.StdEncoding.EncodedLen(len(m.Encoding))+2)
ret[0] = '"'
base64.StdEncoding.Encode(ret[1:len(ret)-1], m.Encoding)
ret[len(ret)-1] = '"'
return ret, nil
}
func (m *EncodedProfile) UnmarshalJSON(s []byte) error {
if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
return fmt.Errorf("not a JSON quoted string: %q", s)
}
b := make([]byte, base64.StdEncoding.DecodedLen(len(s)-2))
n, err := base64.StdEncoding.Decode(b, s[1:len(s)-1])
if err != nil {
return err
}
return m.Unmarshal(b[:n])
}
var _ json.Marshaler = (*EncodedProfile)(nil)
var _ json.Unmarshaler = (*EncodedProfile)(nil)