universe-builder

command module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

README

做了一个简单的go generate工具,根据原有结构体生成对应的builder结构体,内置flag标识,区分指针类型是否赋值

用法

go install gitee.com/LosingBattle/universe-builder@latest

添加 go:generate universe-builder -type struct

//go:generate  universe-builder -type struct
package test

type Foo struct {
	A *int //note
	B *int
}


foo.go 会生成一个文件foo_builder.go


package test

type FooBuilder struct {
	a     int //note
	aFlag bool
	b     int
	bFlag bool
}

func NewFooBuilder() *FooBuilder {
	return &FooBuilder{}
}

func (builder *FooBuilder) A(a int) *FooBuilder {
	builder.a = a
	builder.aFlag = true
	return builder
}
func (builder *FooBuilder) B(b int) *FooBuilder {
	builder.b = b
	builder.bFlag = true
	return builder
}

func (builder *FooBuilder) Build() *Foo {
	req := &Foo{}
	if builder.aFlag {
		req.A = &builder.a
	}
	if builder.bFlag {
		req.B = &builder.b
	}
	return req
}


Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
core

Jump to

Keyboard shortcuts

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