Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ma91n/juv
juv is a code generator for using go-validator in JSON Unmarshall.
https://github.com/ma91n/juv
cli go go-cli go-generate golang
Last synced: about 15 hours ago
JSON representation
juv is a code generator for using go-validator in JSON Unmarshall.
- Host: GitHub
- URL: https://github.com/ma91n/juv
- Owner: ma91n
- License: apache-2.0
- Created: 2019-08-17T12:57:30.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-08-19T09:20:59.000Z (over 5 years ago)
- Last Synced: 2023-04-03T21:56:10.567Z (over 1 year ago)
- Topics: cli, go, go-cli, go-generate, golang
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# juv
juv is a code generator for using [go-validator](https://github.com/go-playground/validator) in JSON Unmarshall.## Installation
Install or upgrade Scaneo with this command.
```bash
go get -u github.com/laqiiz/juv
```## Usage
```bash
juv [options] paths...
```### Example
```bash
juv -o model_gen.go example
```### Options
```console
$ juv -h
Usage of juv:
-o string
-o is output file name (default "juv_gen.go")
-output string
-output is output file name (default "juv_gen.go")
-p string
-p is package name (default "current directory")
-package string
-package is package name (default "current directory")
```## Go Generate
If you want to use juv with go generate, then just add this comment to the top of go file.
```go
//go:generate juv $GOFILE
package modelstype Login struct {
// some fields
}
```Now you can call go generate in package models and model_gen.go will be created.
## Motivation
go-validator is a great library, but it becomes redundant when building HTTP server as follows.
```go
func main() {
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
var login Login
if err := json.NewDecoder(r.Body).Decode(&login); err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
return
}
if err := validator.New().Struct(login); err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
return
}
// ... some logic ...
})
log.Fatal(http.ListenAndServe(":8080", nil))
}// Any logic
```It is possible to simplify by performing validation at the stage of json unmarshall.
```go
func (r *Login) UnmarshalJSON(b []byte) error {
type Alias Login // avoid stack over flow error
var a Alias
if err := json.Unmarshal(b, &a); err != nil {
return err
}if err := validator.New().Struct(a); err != nil {
return err
}r.ID = a.ID
r.Pass = a.Passreturn nil
}
```So 1st code can be simple.
```go
func main() {
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
var login Login
if err := json.NewDecoder(r.Body).Decode(&login); err != nil { // JSON Unmarshall and Validate
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
return
}
// ... some logic ...
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
````## License
This project is licensed under the Apache License 2.0 License - see the [LICENSE](LICENSE) file for details