NewPartialObject creates a PartialObject with the specified base key-value pairs, and allows for setting the values of newKeys when building the final JSON object.
(base map[string]JSON, newKeys []string)
| 475 | // pairs, and allows for setting the values of newKeys when building the final |
| 476 | // JSON object. |
| 477 | func NewPartialObject(base map[string]JSON, newKeys []string) (*PartialObject, error) { |
| 478 | // Check that newKeys are unique and disjoint from base keys. |
| 479 | newKeySet := make(map[string]struct{}, len(newKeys)) |
| 480 | for _, k := range newKeys { |
| 481 | if _, ok := newKeySet[k]; ok { |
| 482 | return nil, errors.AssertionFailedf("expected unique keys, found %v", newKeys) |
| 483 | } |
| 484 | if _, ok := base[k]; ok { |
| 485 | return nil, errors.AssertionFailedf("expected new keys to be disjoint from base keys, found %v in both", k) |
| 486 | } |
| 487 | newKeySet[k] = struct{}{} |
| 488 | } |
| 489 | |
| 490 | keys := slices.Collect(maps.Keys(base)) |
| 491 | keys = append(keys, newKeys...) |
| 492 | sort.Strings(keys) |
| 493 | |
| 494 | pairs := make([]jsonKeyValuePair, 0, len(base)) |
| 495 | keyOrd := make(map[string]int, len(keys)) |
| 496 | |
| 497 | for i, k := range keys { |
| 498 | keyOrd[k] = i |
| 499 | if _, ok := base[k]; !ok { |
| 500 | continue |
| 501 | } |
| 502 | pairs = append(pairs, jsonKeyValuePair{k: jsonString(k), v: base[k]}) |
| 503 | } |
| 504 | |
| 505 | return &PartialObject{basePairs: pairs, keyOrd: keyOrd, allowedNewKeys: newKeySet}, nil |
| 506 | } |
| 507 | |
| 508 | // NewObject creates a new JSON object with the specified new key-values. The |
| 509 | // keys of newData must be exactly the same as the newKeys supplied to the |
nothing calls this directly
no test coverage detected
searching dependent graphs…