UnmarshalJSON implements json.Unmarshaler interface by unmarshaling a custom JSON description.
(bz []byte)
| 390 | // UnmarshalJSON implements json.Unmarshaler interface by unmarshaling a custom |
| 391 | // JSON description. |
| 392 | func (bA *BitArray) UnmarshalJSON(bz []byte) error { |
| 393 | b := string(bz) |
| 394 | if b == "null" { |
| 395 | // This is required e.g. for encoding/json when decoding |
| 396 | // into a pointer with pre-allocated BitArray. |
| 397 | bA.Bits = 0 |
| 398 | bA.Elems = nil |
| 399 | return nil |
| 400 | } |
| 401 | |
| 402 | // Validate 'b'. |
| 403 | match := bitArrayJSONRegexp.FindStringSubmatch(b) |
| 404 | if match == nil { |
| 405 | return fmt.Errorf("bitArray in JSON should be a string of format %q but got %s", bitArrayJSONRegexp.String(), b) |
| 406 | } |
| 407 | bits := match[1] |
| 408 | |
| 409 | // Construct new BitArray and copy over. |
| 410 | numBits := len(bits) |
| 411 | bA2 := NewBitArray(numBits) |
| 412 | for i := 0; i < numBits; i++ { |
| 413 | if bits[i] == 'x' { |
| 414 | bA2.SetIndex(i, true) |
| 415 | } |
| 416 | } |
| 417 | *bA = *bA2 //nolint:govet |
| 418 | return nil |
| 419 | } |
| 420 | |
| 421 | // ToProto converts BitArray to protobuf |
| 422 | func (bA *BitArray) ToProto() *tmprotobits.BitArray { |
nothing calls this directly
no test coverage detected