https://github.com/emptyless/nullify
Return the pointer version of any input (including changing the fields of structs to pointer versions)
https://github.com/emptyless/nullify
Last synced: about 2 months ago
JSON representation
Return the pointer version of any input (including changing the fields of structs to pointer versions)
- Host: GitHub
- URL: https://github.com/emptyless/nullify
- Owner: Emptyless
- License: mit
- Created: 2024-03-27T08:25:19.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-10T11:00:33.000Z (12 months ago)
- Last Synced: 2024-06-22T03:39:34.747Z (11 months ago)
- Language: Go
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nullify
Returns the pointer version of any input recursively including e.g. field structs (while retaining tags). This is especially
useful in e.g. JSON serialization/deserialization in combination with a struct validator to check if a field was sent or not
without changing the struct.## Install
Use `github.com/Emptyless/nullify` to download the latest version.
## Example
To quickly get up and running, run e.g. the following:
```go
package mainimport (
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
)type Person struct {
Name string `json:"name" validate:"required"`
}func main() {
input := []byte("")
person := Person{}
p := Nullify(person)
_ = json.Unmarshal(input, p)
err := validator.New().Struct(p)
fmt.Println(err)
// Output:
// Key: 'Name' Error:Field validation for 'Name' failed on the 'required' tag
}
```For the full example, see `/example` for an example with [go-playground/validator](https://github.com/go-playground/validator).