Help us shape the future of Go! Take the 2026 Go Developer Survey

jwg

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2019 License: MIT Imports: 6 Imported by: 0

README

jwg

JSON wrapper generator or json web go.

Description

jwg can generate Web API friendly struct.

We need a lowerCamelCase property name, not a UpperCamelCase. jwg generate json tag with lower camel case automatically.

Another case, We need a access control of struct property in web app.

type User struct {
	Name              string
	EncryptedPassword string // I want to hide this field!
}
user := &User {
	"go-chan",
	"5ch",
}
builder := NewUserJSONBuilder()
builder.AddAll()
builder.Remove(builder.EncryptedPassword)
json, _ := builder.Marshal(user)
// emit `{"name":"go-chan"}`
fmt.Println(string(json))
Example

from:

type Sample struct {
	Foo string
}

to:

// generated!
type SampleJSON struct {
	Foo string `json:"foo,omitempty"`
}

usage:

src := &Sample{"Foo!"}

builder := NewSampleJSONBuilder() // generated!
builder.AddAll()
var jsonStruct *SampleJSON = builder.Convert(src)

json, err := json.Marshal(jsonStruct)

other example.

With go generate
$ ls -la .
total 16
drwxr-xr-x@ 4 vvakame  staff   136  1 27 17:27 .
drwxr-xr-x@ 6 vvakame  staff   204  1 27 13:14 ..
-rw-r--r--@ 1 vvakame  staff   366  1 27 17:27 model.go
$ cat model.go
//go:generate jwg -output model_json.go .

package d

// test for struct with tagged comment

// +jwg
type Sample struct {
	A string `json:"foo!" datastore:"-"`
	B string `json:",omitempty" datastore:",noindex" endpoints:"req"`
}
$ go generate
$ ls -la .
total 16
drwxr-xr-x@ 4 vvakame  staff   136  1 27 17:27 .
drwxr-xr-x@ 6 vvakame  staff   204  1 27 13:14 ..
-rw-r--r--@ 1 vvakame  staff   366  1 27 17:27 model.go
-rw-r--r--@ 1 vvakame  staff  1549  1 27 17:27 model_json.go

Installation

$ go get github.com/favclip/jwg/cmd/jwg
$ jwg
Usage of jwg:
	jwg [flags] [directory]
	jwg [flags] files... # Must be a single package
Flags:
  -output="": output file name; default srcdir/<type>_string.go
  -type="": comma-separated list of type names; must be set

Command sample

Model with type specific option.

$ cat misc/fixture/a/model.go
package a

// test for basic struct definition

type Sample struct {
	Foo string
}
$ jwg -type Sample -output misc/fixture/a/model_json.go misc/fixture/a

Model with tagged comment.

$ cat misc/fixture/d/model.go
package d

// test for struct with tagged comment

// +jwg
type Sample struct {
	A string `json:"foo!" datastore:"-"`
	B string `json:",omitempty" datastore:",noindex" endpoints:"req"`
}

type IgnoredSample struct {
	A string `json:"foo!" datastore:"-"`
	B string `json:",omitempty" datastore:",noindex" endpoints:"req"`
}
$ jwg -output misc/fixture/d/model_json.go misc/fixture/d

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidTranscriptTags = errors.New("do not contains json tag in transcriptTagNames")

ErrInvalidTranscriptTags means invalid transcriptTag included.

Functions

This section is empty.

Types

type BuildField

type BuildField struct {
	Name           string
	Embed          bool
	Tag            *BuildTag
	TranscriptTags []string
	// contains filtered or unexported fields
}

BuildField represents field of BuildStruct.

func (*BuildField) IsArray

func (f *BuildField) IsArray() bool

IsArray returns field type is array.

func (*BuildField) IsArrayPtr

func (f *BuildField) IsArrayPtr() bool

IsArrayPtr returns field type is array of pointer.

func (*BuildField) IsPtr

func (f *BuildField) IsPtr() bool

IsPtr returns field type is pointer.

func (*BuildField) IsPtrArray

func (f *BuildField) IsPtrArray() bool

IsPtrArray returns field type is pointer array.

func (*BuildField) IsPtrArrayPtr

func (f *BuildField) IsPtrArrayPtr() bool

IsPtrArrayPtr returns field type is pointer of pointer array.

func (*BuildField) WithJWG

func (f *BuildField) WithJWG() bool

WithJWG returns is field jwg annotated type.

type BuildSource

type BuildSource struct {
	Structs []*BuildStruct
	// contains filtered or unexported fields
}

BuildSource represents source code of assembling..

func Parse

func Parse(pkg *genbase.PackageInfo, typeInfos genbase.TypeInfos, opts *ParseOptions) (*BuildSource, error)

Parse construct *BuildSource from package & type information.

func (*BuildSource) Emit

func (b *BuildSource) Emit(args *[]string) ([]byte, error)

Emit generate wrapper code.

type BuildStruct

type BuildStruct struct {
	Fields []*BuildField
	// contains filtered or unexported fields
}

BuildStruct represents struct of assembling..

func (*BuildStruct) Name

func (st *BuildStruct) Name() string

Name returns struct type name.

type BuildTag

type BuildTag struct {
	Name       string
	Ignore     bool // e.g. Secret string `json:"-"`
	OmitEmpty  bool // e.g. Field int `json:",omitempty"`
	ShouldEmit bool // e.g. Field int `json:",shouldemit"` shouldemit is not official supported tag.
	String     bool // e.g. Int64String int64 `json:",string"`
	// contains filtered or unexported fields
}

BuildTag represents tag of BuildField.

func (*BuildTag) TagString

func (tag *BuildTag) TagString() string

TagString build tag string.

type ParseOptions

type ParseOptions struct {
	TranscriptTagNames    []string
	NoOmitempty           bool
	NoOmitemptyFieldTypes []string
}

ParseOptions means options to Parse function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL