GithubHelp home page GithubHelp logo

nathanusask / gormaid Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 100 KB

This is a tool for go projects to automatically generate boilerplate code for CRUD methods using gorm.

License: Apache License 2.0

Go 100.00%

gormaid's Introduction

gormaid

This is a tool for go projects to automatically generate boilerplate code for CRUD methods using gorm.

The problem

Suppose in the following code file we are declaring gorm structs

type Position struct {
    HouseID string `json:"house_id" gorm:"primaryKey;comment:房间号"`
    ShelfID string `json:"shelf_id" gorm:"primaryKey;comment:笼架号"`
    X       string `json:"x" gorm:"primaryKey;comment:横坐标"`
    Y       string `json:"y" gorm:"primaryKey;comment:纵坐标"`
}

type Transfer struct {
	TaskID               uint      `json:"task_id" gorm:"primaryKey;comment:任务id"`
	Mice                 []string  `json:"mice,omitempty" binding:"required" gorm:"serializer:json;comment:小鼠编号"`
	SourceCageID         uint      `json:"src_cage_id,omitempty" gorm:"column:src_cage_id;primaryKey;comment:来源笼位"`
	SourcePosition       *Position `json:"src,omitempty" gorm:"embedded;embeddedPrefix:src_"`
	TargetCageID         uint      `json:"dest_cage_id,omitempty" gorm:"column:dest_cage_id;primaryKey;comment:目的笼位id"`
	TargetPosition       *Position `json:"dest,omitempty" gorm:"embedded;embeddedPrefix:dest_"`
	SourceFeeder         uint      `json:"source_feeder,omitempty" gorm:"comment:来源房间饲养员"`
	TargetFeeder         uint      `json:"target_feeder,omitempty" gorm:"comment:目的房间饲养员"`
}

whose schemas will be automatically created thanks to GORM. However, professional go developers tend to find it tedious and error-prone work to exhaustively write code for CRUD methods. For instance, in order to query for a Transfer record using any field, one may exhaust all 14 fields including the one in the embedded Position struct. The development work amount gets exploded when more structs are coming and they have to be thoroughly tested prior to going production.

The goal

We've talked about the pain, and we need a solution. What about a go:generate command that takes away all our pains? Things like

//go:generate gormaid -struct Transfer -package tasktransfer -o postgres/transfer_crud.go
type Transfer struct {
	TaskID               uint      `json:"task_id" gorm:"primaryKey;comment:任务id"`
	Mice                 []string  `json:"mice,omitempty" binding:"required" gorm:"serializer:json;comment:小鼠编号"`
	SourceCageID         uint      `json:"src_cage_id,omitempty" gorm:"column:src_cage_id;primaryKey;comment:来源笼位"`
	SourcePosition       *Position `json:"src,omitempty" gorm:"embedded;embeddedPrefix:src_"`
	TargetCageID         uint      `json:"dest_cage_id,omitempty" gorm:"column:dest_cage_id;primaryKey;comment:目的笼位id"`
	TargetPosition       *Position `json:"dest,omitempty" gorm:"embedded;embeddedPrefix:dest_"`
	SourceFeeder         uint      `json:"source_feeder,omitempty" gorm:"comment:来源房间饲养员"`
	TargetFeeder         uint      `json:"target_feeder,omitempty" gorm:"comment:目的房间饲养员"`
}

where -struct Transfer means the struct to be parsed; -package tasktransfer means the output package name; -o postgres/transfer_crud.go means the relative output path of the generated file.

gormaid's People

Contributors

nathanusask avatar

Watchers

 avatar

gormaid's Issues

RFC

Write up an RFC for the project as a starter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.