ma91n / juv

juv is a code generator for using go-validator in JSON Unmarshall.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

juv

juv is a code generator for using go-validator in JSON Unmarshall.

Installation

Install or upgrade Scaneo with this command.

go get -u github.com/laqiiz/juv

Usage

juv [options] paths...

Example

juv -o model_gen.go example

Options

$ 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:generate juv $GOFILE
package models

type 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.

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.

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.Pass

	return nil
}

So 1st code can be simple.

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 file for details

About

juv is a code generator for using go-validator in JSON Unmarshall.

License:Apache License 2.0


Languages

Language:Go 100.0%