--------------------------------------------------------------------------- Shared JSON masking utilities --------------------------------------------------------------------------- maskDocumentString unmarshals a JSON string, masks it recursively using the ObjectSchema and semantic type maskers, an
(document string, objectSchema *storepb.ObjectSchema, semanticTypeToMasker map[string]masker.Masker)
| 254 | // maskDocumentString unmarshals a JSON string, masks it recursively using |
| 255 | // the ObjectSchema and semantic type maskers, and marshals it back. |
| 256 | func maskDocumentString(document string, objectSchema *storepb.ObjectSchema, semanticTypeToMasker map[string]masker.Masker) (string, error) { |
| 257 | if document == "" || objectSchema == nil { |
| 258 | return document, nil |
| 259 | } |
| 260 | |
| 261 | var parsed any |
| 262 | if err := json.Unmarshal([]byte(document), &parsed); err != nil { |
| 263 | return "", errors.Wrap(err, "failed to unmarshal document") |
| 264 | } |
| 265 | |
| 266 | masked, err := walkAndMaskJSONRecursive(parsed, objectSchema, semanticTypeToMasker) |
| 267 | if err != nil { |
| 268 | return "", errors.Wrap(err, "failed to mask document") |
| 269 | } |
| 270 | |
| 271 | out, err := json.Marshal(masked) |
| 272 | if err != nil { |
| 273 | return "", errors.Wrap(err, "failed to marshal masked document") |
| 274 | } |
| 275 | return string(out), nil |
| 276 | } |
| 277 | |
| 278 | func walkAndMaskJSON(data map[string]any, fieldPaths map[string][]*parserbase.PathAST, objectSchema *storepb.ObjectSchema, semanticTypeToMasker map[string]masker.Masker) (map[string]any, error) { |
| 279 | result := make(map[string]any) |