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