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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 } | 210 } |
211 return time.Unix(int64(v/1e6), int64((v%1e6)*1e3)).UTC(), nil | 211 return time.Unix(int64(v/1e6), int64((v%1e6)*1e3)).UTC(), nil |
212 } | 212 } |
213 | 213 |
214 // WriteProperty writes a Property to the buffer. `context` behaves the same | 214 // WriteProperty writes a Property to the buffer. `context` behaves the same |
215 // way that it does for WriteKey, but only has an effect if `p` contains a | 215 // way that it does for WriteKey, but only has an effect if `p` contains a |
216 // Key as its Value. | 216 // Key as its Value. |
217 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) { | 217 func WriteProperty(buf Buffer, context KeyContext, p ds.Property) (err error) { |
218 defer recoverTo(&err) | 218 defer recoverTo(&err) |
219 typb := byte(p.Type()) | 219 typb := byte(p.Type()) |
220 » if p.IndexSetting() == ds.NoIndex { | 220 » if p.IndexSetting() != ds.NoIndex { |
221 typb |= 0x80 | 221 typb |= 0x80 |
222 } | 222 } |
223 panicIf(buf.WriteByte(typb)) | 223 panicIf(buf.WriteByte(typb)) |
224 switch p.Type() { | 224 switch p.Type() { |
225 case ds.PTNull, ds.PTBoolTrue, ds.PTBoolFalse: | 225 case ds.PTNull, ds.PTBoolTrue, ds.PTBoolFalse: |
226 case ds.PTInt: | 226 case ds.PTInt: |
227 _, err = cmpbin.WriteInt(buf, p.Value().(int64)) | 227 _, err = cmpbin.WriteInt(buf, p.Value().(int64)) |
228 case ds.PTFloat: | 228 case ds.PTFloat: |
229 _, err = cmpbin.WriteFloat64(buf, p.Value().(float64)) | 229 _, err = cmpbin.WriteFloat64(buf, p.Value().(float64)) |
230 case ds.PTString: | 230 case ds.PTString: |
(...skipping 19 matching lines...) Expand all Loading... |
250 // ReadProperty reads a Property from the buffer. `context`, `appid`, and | 250 // ReadProperty reads a Property from the buffer. `context`, `appid`, and |
251 // `namespace` behave the same way they do for ReadKey, but only have an | 251 // `namespace` behave the same way they do for ReadKey, but only have an |
252 // effect if the decoded property has a Key value. | 252 // effect if the decoded property has a Key value. |
253 func ReadProperty(buf Buffer, context KeyContext, appid, namespace string) (p ds
.Property, err error) { | 253 func ReadProperty(buf Buffer, context KeyContext, appid, namespace string) (p ds
.Property, err error) { |
254 val := interface{}(nil) | 254 val := interface{}(nil) |
255 typb, err := buf.ReadByte() | 255 typb, err := buf.ReadByte() |
256 if err != nil { | 256 if err != nil { |
257 return | 257 return |
258 } | 258 } |
259 is := ds.ShouldIndex | 259 is := ds.ShouldIndex |
260 » if (typb & 0x80) != 0 { | 260 » if (typb & 0x80) == 0 { |
261 is = ds.NoIndex | 261 is = ds.NoIndex |
262 } | 262 } |
263 switch ds.PropertyType(typb & 0x7f) { | 263 switch ds.PropertyType(typb & 0x7f) { |
264 case ds.PTNull: | 264 case ds.PTNull: |
265 case ds.PTBoolTrue: | 265 case ds.PTBoolTrue: |
266 val = true | 266 val = true |
267 case ds.PTBoolFalse: | 267 case ds.PTBoolFalse: |
268 val = false | 268 val = false |
269 case ds.PTInt: | 269 case ds.PTInt: |
270 val, _, err = cmpbin.ReadInt(buf) | 270 val, _, err = cmpbin.ReadInt(buf) |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 } | 557 } |
558 } | 558 } |
559 | 559 |
560 func recoverTo(err *error) { | 560 func recoverTo(err *error) { |
561 if r := recover(); r != nil { | 561 if r := recover(); r != nil { |
562 if rerr := r.(parseError); rerr != nil { | 562 if rerr := r.(parseError); rerr != nil { |
563 *err = error(rerr) | 563 *err = error(rerr) |
564 } | 564 } |
565 } | 565 } |
566 } | 566 } |
OLD | NEW |