OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package serialize | 5 package serialize |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "errors" | 9 "errors" |
10 "fmt" | 10 "fmt" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 | 182 |
183 gp.Lng, _, e = cmpbin.ReadFloat64(buf) | 183 gp.Lng, _, e = cmpbin.ReadFloat64(buf) |
184 panicIf(e) | 184 panicIf(e) |
185 | 185 |
186 if !gp.Valid() { | 186 if !gp.Valid() { |
187 err = fmt.Errorf("helper: decoded invalid GeoPoint: %v", gp) | 187 err = fmt.Errorf("helper: decoded invalid GeoPoint: %v", gp) |
188 } | 188 } |
189 return | 189 return |
190 } | 190 } |
191 | 191 |
192 // timeToInt converts a time value to an integer value. See WriteTime for more | |
193 // details. | |
194 func timeToInt(t time.Time) int64 { | |
195 t = t.Round(time.Microsecond) | |
196 return t.Unix()*1e6 + int64(t.Nanosecond()/1e3) | |
197 } | |
198 | |
192 // WriteTime writes a time.Time in a byte-sortable way. | 199 // WriteTime writes a time.Time in a byte-sortable way. |
193 // | 200 // |
194 // This method truncates the time to microseconds and drops the timezone, | 201 // This method truncates the time to microseconds and drops the timezone, |
195 // because that's the (undocumented) way that the appengine SDK does it. | 202 // because that's the (undocumented) way that the appengine SDK does it. |
196 func WriteTime(buf Buffer, t time.Time) error { | 203 func WriteTime(buf Buffer, t time.Time) error { |
197 name, off := t.Zone() | 204 name, off := t.Zone() |
198 if name != "UTC" || off != 0 { | 205 if name != "UTC" || off != 0 { |
199 panic(fmt.Errorf("helper: UTC OR DEATH: %s", t)) | 206 panic(fmt.Errorf("helper: UTC OR DEATH: %s", t)) |
200 } | 207 } |
201 » _, err := cmpbin.WriteInt(buf, t.Unix()*1e6+int64(t.Nanosecond()/1e3)) | 208 » _, err := cmpbin.WriteInt(buf, timeToInt(t)) |
202 return err | 209 return err |
203 } | 210 } |
204 | 211 |
205 // ReadTime reads a time.Time from the buffer. | 212 // ReadTime reads a time.Time from the buffer. |
206 func ReadTime(buf Buffer) (time.Time, error) { | 213 func ReadTime(buf Buffer) (time.Time, error) { |
207 v, _, err := cmpbin.ReadInt(buf) | 214 v, _, err := cmpbin.ReadInt(buf) |
208 if err != nil { | 215 if err != nil { |
209 return time.Time{}, err | 216 return time.Time{}, err |
210 } | 217 } |
211 t := time.Unix(v/1e6, (v%1e6)*1e3) | 218 t := time.Unix(v/1e6, (v%1e6)*1e3) |
212 if t.IsZero() { | 219 if t.IsZero() { |
213 return time.Time{}, nil | 220 return time.Time{}, nil |
214 } | 221 } |
215 return t.UTC(), nil | 222 return t.UTC(), nil |
216 } | 223 } |
217 | 224 |
218 // WriteProperty writes a Property to the buffer. `context` behaves the same | 225 // WriteProperty writes a Property to the buffer. `context` behaves the same |
219 // way that it does for WriteKey, but only has an effect if `p` contains a | 226 // way that it does for WriteKey, but only has an effect if `p` contains a |
220 // Key as its Value. | 227 // Key as its Value. |
221 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) { | 228 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) { |
222 defer recoverTo(&err) | 229 defer recoverTo(&err) |
223 » typb := byte(p.Type()) | 230 |
231 » effectiveType := p.Type() | |
232 » switch effectiveType { | |
233 » case ds.PTTime: | |
dnj
2015/12/29 16:48:09
I made this a switch statement in case there were
| |
234 » » effectiveType = ds.PTInt | |
235 » } | |
236 » typb := byte(effectiveType) | |
iannucci
2015/12/29 18:29:20
This change is not good; it will call property map
| |
237 | |
224 if p.IndexSetting() != ds.NoIndex { | 238 if p.IndexSetting() != ds.NoIndex { |
225 typb |= 0x80 | 239 typb |= 0x80 |
226 } | 240 } |
227 panicIf(buf.WriteByte(typb)) | 241 panicIf(buf.WriteByte(typb)) |
228 switch p.Type() { | 242 switch p.Type() { |
229 case ds.PTNull: | 243 case ds.PTNull: |
230 case ds.PTBool: | 244 case ds.PTBool: |
231 b := p.Value().(bool) | 245 b := p.Value().(bool) |
232 if b { | 246 if b { |
233 err = buf.WriteByte(1) | 247 err = buf.WriteByte(1) |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 } | 607 } |
594 } | 608 } |
595 | 609 |
596 func recoverTo(err *error) { | 610 func recoverTo(err *error) { |
597 if r := recover(); r != nil { | 611 if r := recover(); r != nil { |
598 if rerr := r.(parseError); rerr != nil { | 612 if rerr := r.(parseError); rerr != nil { |
599 *err = error(rerr) | 613 *err = error(rerr) |
600 } | 614 } |
601 } | 615 } |
602 } | 616 } |
OLD | NEW |