MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / assertStructuredFieldAppendable

Function assertStructuredFieldAppendable

internal/models/item.go:1037–1064  ·  view source on GitHub ↗

assertStructuredFieldAppendable refuses an append when key holds a value that does not decode into []T. BUG-2627. The Append* helpers below build the new slice from Extract*, then assign it over the key unconditionally. Extract* returns nil for FOUR different reasons and only one of them is a defec

(fieldsMap map[string]any, key string)

Source from the content-addressed store, hash-verified

1035// divergence this function exists to prevent. Both directions are pinned by
1036// tests; see TestAppendDecisionLogRefusesEntriesOnlyItsOwnTypeRejects.
1037func assertStructuredFieldAppendable[T any](fieldsMap map[string]any, key string) error {
1038 raw, ok := fieldsMap[key]
1039 if !ok {
1040 return nil // case 1 — absent, nothing to lose.
1041 }
1042 if raw == nil {
1043 return nil // an explicit JSON null carries no entries either.
1044 }
1045
1046 payload, err := json.Marshal(raw)
1047 if err != nil {
1048 return fmt.Errorf("%w: %q could not be re-encoded for inspection: %w", ErrStructuredFieldUnreadable, key, err)
1049 }
1050
1051 var entries []T
1052 if err := json.Unmarshal(payload, &entries); err != nil {
1053 // Deliberately does NOT name a repair command. The remedy has to be
1054 // one that works in THIS state, and every append path is exactly what
1055 // is being refused here; `pad item show --format json` is read-only
1056 // and does surface the raw value, so it is the one action safe to
1057 // suggest.
1058 return fmt.Errorf(
1059 "%w: %q holds a value that is not a list of entries, so appending would overwrite and destroy it; "+
1060 "inspect the stored value with `pad item show <ref> --format json` and repair it before appending (BUG-2627)",
1061 ErrStructuredFieldUnreadable, key)
1062 }
1063 return nil
1064}
1065
1066// unreadableFieldsBlob tags an append's fields-parse failure as
1067// ErrStructuredFieldUnreadable.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected