tools

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2019 License: MIT Imports: 11 Imported by: 0

README

package dbs

目标:

a tool to generate Model struct to service Struct like convert UserModel to UserService

type UserModel struct {
	Name     string `gorm:"column:name"`
	Password string `gorm:"column:password"`
}

type UserService struct {
	Name     string `json:"name"`
	Password string `json:"password"`
}

使用方法

➜  server-common git:(master) ✗ ./dto -h
Usage of ./dto:
  -debug
        debug mode, if open this will output info
  -dir string
        model directory from where
  -filename string
        specified filename those you want to generate
  -generateDir string
        generate file will be saved here (default ".")
  -generateFilename string
        generate file name will be use this (default "types.go")
  -generatePkgName string
        generatePkgName (default "types")
  -generateStructSuffix string
        replace model struct name suffix, like: UserSuffix => User
  -modelImportPath my-server/models
        model package path, cannot be empty, like my-server/models
  -modelStructSuffix string
        specified in which Model name style can be generate (default "Model")

直接安装

go get github.com/yeqown/server-common/dbs/tools/cmd/dto

安装

go get github.com/yeqown/server-common/dbs/tools
# 获取 tool.main.go, 并选择性的实现自己的 CustomParseTagFunc & CustomGenerateTagFunc
go build -o dto tool.main.go

使用实例

# 根据./dbs/tools/testdata目录下的type_model.go生成文件,
# 在./dbs/tools/testdata目录下生成文件,
# 指定包名为testdata
dto -dir=./dbs/tools/testdata -filename=type_model.go -generatePkgName=testdata 
-generateDir=./dbs/tools/testdata -modelImportPath=model

# 多文件可以使用多次 -filename=model1.go -filename=model2.go
// type_model.go
package testdata

import (
	"errors"
	"time"
)

type UserModel struct {
	Name       string    `gorm:"colunm:name"`
	Password   string    `gorm:"column:password"`
	CreateTime time.Time `gorm:"colunm:create_time"`
	UpdateTime time.Time `gorm:"colunm:update_time"`
}

type userStruct struct {
	Name     string `json:"typeStructName"`
	Password string `json:"typeStructPassword"`
}

type AliasString string

var (
	A UserModel
	B int64
	C string
)

func f(a string) error {
	return errors.New(a)
}

// 生成的文件 types.go[未被格式化]

// Package testdata ...
// Generate by github.com/yeqown/server-common/dbs/tools
package testdata

import (
	"model"
)

// User description here
type User struct {
	Name string `json:"name"`
	Password string `json:"password"`
	CreateTime time.Time `json:"create_time"`
	UpdateTime time.Time `json:"update_time"`
	}
// LoadUserFromModel func to load data from model
func LoadUserFromModel(data *testdata.UserModel) *User {
	return &User {
		Name: data.Name,
		Password: data.Password,
		CreateTime: data.CreateTime,
		UpdateTime: data.UpdateTime,
		}
}

配置自定义Tag解析和生成函数

tool.main.go文件中已经定义了

func main() {
	// set custom funcs
	tools.SetCustomGenTagFunc(CustomGenerateTagFunc)
	tools.SetCustomParseTagFunc(CustomParseTagFunc)
}

// CustomParseTagFunc to custom implment yourself parseTagFunc
// @param (gorm:"colunm:name")
// return ("name")
func CustomParseTagFunc(s string) string {
	log.Println("calling  CustomParseTagFunc", s)

	s = strings.Replace(s, `"`, "", -1)
	splited := strings.Split(s, ":")
	return splited[len(splited)-1]
}

// CustomGenerateTagFunc to implment yourself generateTagFunc
// @param name fieldName (Age, Name, Year, CreateTime)
// @param typ fieldType (string, int64, time.Time)
// @param tag (CustomParseTagFunc) return value, default is gorm tag
// return (json:"name")
func CustomGenerateTagFunc(name, typ, tag string) string {
	return fmt.Sprintf("json:\"%s\"", tag)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseAndGenerate

func ParseAndGenerate(cfg *UsageCfg) error

ParseAndGenerate parse all input go files and get wanted struct info save with innerStruct, and then generate file

func SetCustomGenTagFunc

func SetCustomGenTagFunc(f genTagFunc)

SetCustomGenTagFunc use user's custom genTagFunc

func SetCustomParseTagFunc

func SetCustomParseTagFunc(f parseTagFunc)

SetCustomParseTagFunc use user's custom parseTag func

Types

type UsageCfg

type UsageCfg struct {
	// Dir 设置需要对那一路径下的文件进行解析
	Dir string
	// ExportDir 设置生成的文件的存放地址
	ExportDir string
	// ExportFilename 指定生成的文件名字
	ExportFilename string
	// ExportPkgName 指定生成的文件的包名
	ExportPkgName string
	// ExportStructSuffix 指定生成的新的机构体后缀
	ExportStructSuffix string
	// ModelImportPath 指定源文件所在包的导入路径
	ModelImportPath string
	// StructSuffix 需要解析的自定义结构体后缀
	StructSuffix string
	// Debug 调试模式开关
	Debug bool
	// Filenames 指定需要解析的.go源文件 文件名字
	Filenames []string
}

UsageCfg ... config tools some feature

Directories

Path Synopsis
cmd
dto

Jump to

Keyboard shortcuts

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