https://github.com/tomoyamachi/go-mask-json-patterns
https://github.com/tomoyamachi/go-mask-json-patterns
go json
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tomoyamachi/go-mask-json-patterns
- Owner: tomoyamachi
- License: mit
- Created: 2020-06-24T00:56:20.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-18T06:00:08.000Z (over 1 year ago)
- Last Synced: 2025-03-25T09:49:24.491Z (over 1 year ago)
- Topics: go, json
- Language: Go
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-mask-json-patterns
A collection of patterns for masking sensitive fields in JSON output with Go.
## Patterns
| Package | Approach | Description |
|---|---|---|
| `structtag_interface` | Struct tag + reflection | Mask fields marked with `log:"*"` tag |
| `structtag_alias` | Struct tag + alias type | Mask fields marked with `sensitive:"true"` tag |
| `interfaces` | JSON key path | Mask fields in a JSON string by specifying key paths |
| `override` | MarshalJSON/String override | Manually implement masking per struct |
| `originaltype` | Custom type | Define a dedicated type that always masks its value |
## Example (`structtag_interface`)
### Struct Definition
```go
type MaskResponse struct {
Str string `json:"str,omitempty"`
MaskStr string `json:"mstr,omitempty" log:"*"`
Int int `json:"int,omitempty"`
MaskInt int `json:"mint,omitempty" log:"*"`
Slice []string `json:"slice,omitempty"`
MaskSlice []string `json:"mslice,omitempty" log:"*"`
Map map[string]string `json:"map,omitempty"`
MaskMap map[string]string `json:"mmap,omitempty" log:"*"`
Struct SubMask `json:"struct,omitempty"`
MaskStruct SubMask `json:"mstruct,omitempty" log:"*"`
PointerStruct *SubMask `json:"pstruct,omitempty"`
MaskPointerStruct *SubMask `json:"mpstruct,omitempty" log:"*"`
}
type SubMask struct {
Str string `json:"str,omitempty"`
MaskStr string `json:"mstr,omitempty" log:"*"`
}
```
### Masked Output
```json
{
"str": "a",
"mstr": "*",
"int": 100,
"mint": "*",
"slice": [
"a"
],
"mslice": "*",
"map": {
"a": "b"
},
"mmap": "*",
"struct": {
"mstr": "*",
"str": "a"
},
"mstruct": "*",
"pstruct": {
"mstr": "*",
"str": "a"
},
"mpstruct": "*"
}
```