generator

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2020 License: MIT Imports: 17 Imported by: 0

README

rapidash-generator

Generate automatically rapidash boilerplate functions from sql or go-source

Features

  • Generate rapidash.Marshaler/Unmarshaler functions and rapidash.Struct from your go source

Not Support(but support future)

  • Generate from sql(create tables)

Install

go get github.com/kanataxa/rapidash-generator/cmd/rapi-gen 

Usage

rapi-gen -o ${entity}_rapidash.go ${entity}.go
Usage:
  rapi-gen [OPTIONS]

Application Options:
  -w            force write if file is already exists
  -o, --output= output file name. default: os.Stdout
  -t, --tag=    use tag name (default: db)

Help Options:
  -h, --help    Show this help message

For example, you run rapi-gen with below input code.

type User struct {
	ID        uint64     `db:"id"`
	Name      string     `db:"name" json:"name"`
	CreatedAt time.Time  `db:"created_at"`
	UpdatedAt *time.Time `db:"updated_at"`
	NonTag    uint
}

type Value uint32
type StrongItem struct {
	ID    uint64  `db:"id"`
	Name  *string `db:"name"`
	Value Value   `db:"value"`
}

type NonDBStruct struct {
	Power uint64
}

rapi-gen generates below go code.

package entity

import (
	"go.knocknote.io/rapidash"
	"golang.org/x/xerrors"
)

func (e *StrongItem) Struct() *rapidash.Struct {
	return rapidash.NewStruct("strong_items").
		FieldUint64("id").
		FieldStringPtr("name").
		FieldUint32("value")
}
func (e *StrongItem) EncodeRapidash(enc rapidash.Encoder) error {
	enc.Uint64("id", e.ID)
	enc.StringPtr("name", e.Name)
	enc.Uint32("value", uint32(e.Value))
	if err := enc.Error(); err != nil {
		return xerrors.Errorf("failed to encode rapidash: %w", err)
	}
	return nil
}
func (e *StrongItem) DecodeRapidash(dec rapidash.Decoder) error {
	e.ID = dec.Uint64("id")
	e.Name = dec.StringPtr("name")
	e.Value = Value(dec.Uint32("value"))
	if err := dec.Error(); err != nil {
		return xerrors.Errorf("failed to decode rapidash: %w", err)
	}
	return nil
}

func (e *User) Struct() *rapidash.Struct {
	return rapidash.NewStruct("users").
		FieldUint64("id").
		FieldString("name").
		FieldTime("created_at").
		FieldTimePtr("updated_at")
}
func (e *User) EncodeRapidash(enc rapidash.Encoder) error {
	enc.Uint64("id", e.ID)
	enc.String("name", e.Name)
	enc.Time("created_at", e.CreatedAt)
	enc.TimePtr("updated_at", e.UpdatedAt)
	if err := enc.Error(); err != nil {
		return xerrors.Errorf("failed to encode rapidash: %w", err)
	}
	return nil
}
func (e *User) DecodeRapidash(dec rapidash.Decoder) error {
	e.ID = dec.Uint64("id")
	e.Name = dec.String("name")
	e.CreatedAt = dec.Time("created_at")
	e.UpdatedAt = dec.TimePtr("updated_at")
	if err := dec.Error(); err != nil {
		return xerrors.Errorf("failed to decode rapidash: %w", err)
	}
	return nil
}

Author

kanataxa(Sota Itoh)

Notes

This tool is not still stable.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(path string, config *Config) error

Types

type Config

type Config struct {
	ShouldOverwrite bool
	FilePath        string
	Output          string
	Tag             string
}

type Field

type Field struct {
	DBName string
	// contains filtered or unexported fields
}

func (*Field) DecodeFunc

func (f *Field) DecodeFunc() string

func (*Field) EncodeFunc

func (f *Field) EncodeFunc() string

func (*Field) FieldFunc

func (f *Field) FieldFunc() string

func (*Field) IsWrapType

func (f *Field) IsWrapType() bool

func (*Field) Name

func (f *Field) Name() string

func (*Field) UnwrapType

func (f *Field) UnwrapType() string

func (*Field) WrapType

func (f *Field) WrapType() string

type FunctionGenerator

type FunctionGenerator interface {
	Generate() ([]byte, error)
}

func Parse

func Parse(fpath, tagField string) (FunctionGenerator, error)

type GoSourceGenerator

type GoSourceGenerator struct {
	Structs []*Struct
}

func (*GoSourceGenerator) Generate

func (g *GoSourceGenerator) Generate() ([]byte, error)

func (*GoSourceGenerator) Package

func (g *GoSourceGenerator) Package() string

type Struct

type Struct struct {
	Fields []*Field
	// contains filtered or unexported fields
}

func (*Struct) Name

func (s *Struct) Name() string

func (*Struct) Package

func (s *Struct) Package() string

func (*Struct) TableName

func (s *Struct) TableName() string

Directories

Path Synopsis
cmd
Package statik contains static assets.
Package statik contains static assets.

Jump to

Keyboard shortcuts

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