All Projects → moznion → gonstructor

moznion / gonstructor

Licence: BSD-3-Clause license
A command-line tool to generate a constructor for the struct.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to gonstructor

qp-arduino
QP real-time embedded frameworks/RTOS for Arduino (AVR and SAM)
Stars: ✭ 37 (-32.73%)
Mutual labels:  code-generation
toast
Plugin-driven CLI utility for code generation using Go source as IDL
Stars: ✭ 52 (-5.45%)
Mutual labels:  code-generation
sample-generator
Xcode Source Editor Extension to generate Swift model samples
Stars: ✭ 19 (-65.45%)
Mutual labels:  code-generation
kobby
Kobby is a codegen plugin of Kotlin DSL Client by GraphQL schema. The generated DSL supports execution of complex GraphQL queries, mutation and subscriptions in Kotlin with syntax similar to native GraphQL syntax.
Stars: ✭ 52 (-5.45%)
Mutual labels:  code-generation
Beef
Business Entity Execution Framework
Stars: ✭ 95 (+72.73%)
Mutual labels:  code-generation
fling
A fluent API generator
Stars: ✭ 20 (-63.64%)
Mutual labels:  code-generation
gamma
An Eclipse-based modeling framework for the component-based design and analysis of reactive systems
Stars: ✭ 21 (-61.82%)
Mutual labels:  code-generation
code-fold
Write the pattern, then let your code write itself.
Stars: ✭ 13 (-76.36%)
Mutual labels:  code-generation
AUXify
Introduces macro/meta annotations @ aux, @ self, @ instance, @ apply, @ delegated, @ syntax and String-based type class LabelledGeneric
Stars: ✭ 25 (-54.55%)
Mutual labels:  code-generation
cscg
Code Generation as a Dual Task of Code Summarization.
Stars: ✭ 28 (-49.09%)
Mutual labels:  code-generation
islpy
Python wrapper for isl, an integer set library
Stars: ✭ 58 (+5.45%)
Mutual labels:  code-generation
nmodl
Code Generation Framework For NEURON MODeling Language
Stars: ✭ 42 (-23.64%)
Mutual labels:  code-generation
typed-astunparse
Python 3 AST unparser with type comments support.
Stars: ✭ 27 (-50.91%)
Mutual labels:  code-generation
Sweet.apex
Next Generation of Apex Development
Stars: ✭ 43 (-21.82%)
Mutual labels:  code-generation
granate
Code generator for graphql
Stars: ✭ 21 (-61.82%)
Mutual labels:  code-generation
EasyEE-Auto
EasyEE 自动化代码生成器。EasyEE Automated code generator.
Stars: ✭ 39 (-29.09%)
Mutual labels:  code-generation
FSharpWrap
Utility that automatically generates F# modules and functions based on your F# project file's references
Stars: ✭ 14 (-74.55%)
Mutual labels:  code-generation
eaf-linter
🤪 A linter, prettier, and test suite that does everything as-simple-as-possible.
Stars: ✭ 17 (-69.09%)
Mutual labels:  code-generation
Textrude
Code generation from YAML/JSON/CSV models via SCRIBAN templates
Stars: ✭ 79 (+43.64%)
Mutual labels:  code-generation
designto-code
Design to code engine. A design ✌️ code standard. Supports React, Flutter and more.
Stars: ✭ 87 (+58.18%)
Mutual labels:  code-generation

gonstructor

A command-line tool to generate a constructor for the struct.

Installation

$ go get -u github.com/moznion/gonstructor/cmd/gonstructor

Also, you can get the pre-built binaries on Releases.

Or get it with gobinaries.com:

curl -sf https://gobinaries.com/moznion/gonstructor | sh

Dependencies

gonstructor depends on goimports for fixing import paths and formatting code, you need to install it:

$ go get golang.org/x/tools/cmd/goimports

Usage

Usage of gonstructor:
  -constructorTypes string
        [optional] comma-separated list of constructor types; it expects "allArgs" and "builder" (default "allArgs")
  -init string
        [optional] name of function to call on object after creating it
  -output string
        [optional] output file name (default "srcdir/<type>_gen.go")
  -type string
        [mandatory] a type name
  -version
        [optional] show the version information
  -withGetter
        [optional] generate a constructor along with getter functions for each field

Motivation

Data encapsulation is a good practice to make software, and it is necessary to clearly indicate the boundary of the structure by controlling the accessibility of the data fields (i.e. private or public) for that. Basically keeping the data fields be private and immutable would be good to make software be robust because it can avoid unexpected field changing.

Golang has a simple way to do that by choosing the initial character's type: upper case or lower case. Once it has decided to use a field as private, it needs to make something like a constructor function, but golang doesn't have a mechanism to support constructor now.

Therefore this project aims to automatically generate constructors to use structures with private and immutable, easily.

Pre requirements to run

Synopsis

Generate all args constructor

  1. write a struct type with go:generate

e.g.

//go:generate gonstructor --type=Structure --constructorTypes=allArgs"
type Structure struct {
	foo string
	bar io.Reader
	Buz chan interface{}
}
  1. execute go generate ./...
  2. then gonstructor generates a constructor code

e.g.

func NewStructure(foo string, bar io.Reader, buz chan interface{}) *Structure {
	return &Structure{foo: foo, bar: bar, Buz: buz}
}

Generate builder (builder means GoF pattern's one)

  1. write a struct type with go:generate

e.g.

//go:generate gonstructor --type=Structure --constructorTypes=builder"
type Structure struct {
	foo string
	bar io.Reader
	Buz chan interface{}
}
  1. execute go generate ./...
  2. then gonstructor generates a buildr code

e.g.

type StructureBuilder struct {
	foo        string
	bar        io.Reader
	buz        chan interface{}
	bufferSize int
}

func NewStructureBuilder() *StructureBuilder {
	return &StructureBuilder{}
}

func (b *StructureBuilder) Foo(foo string) *StructureBuilder {
	b.foo = foo
	return b
}

func (b *StructureBuilder) Bar(bar io.Reader) *StructureBuilder {
	b.bar = bar
	return b
}

func (b *StructureBuilder) Buz(buz chan interface{}) *StructureBuilder {
	b.buz = buz
	return b
}

func (b *StructureBuilder) BufferSize(bufferSize int) *StructureBuilder {
	b.bufferSize = bufferSize
	return b
}

func (b *StructureBuilder) Build() *Structure {
	return &Structure{
		foo:        b.foo,
		bar:        b.bar,
		Buz:        b.buz,
		bufferSize: b.bufferSize,
	}
}

Call a initializer

  1. write a struct type with go:generate
  2. write a function that initializes internal fields
  3. pass its name to -init parameter

e.g.

//go:generate gonstructor --type=Structure -init construct
type Structure struct {
	foo        string
	bar        io.Reader
	Buz        chan interface{}
	bufferSize int
	buffer     chan []byte `gonstructor:"-"`
}

func (structure *Structure) construct() {
	structure.buffer = make(chan []byte, structure.bufferSize)
}
  1. execute go generate ./...
  2. then gonstructor generates a buildr code

e.g.

func NewStructure(
	foo string,
	bar io.Reader,
	buz chan interface{},
	bufferSize int,
) *Structure {
	r := &Structure{
		foo:        foo,
		bar:        bar,
		Buz:        buz,
		bufferSize: bufferSize,
	}

	r.construct()

	return r
}

How to ignore to contain a field in a constructor

gonstructor:"-" supports that.

e.g.

type Structure struct {
	foo string
	bar int64 `gonstructor:"-"`
}

The generated code according to the above structure doesn't contain bar field.

How to build binaries

Binaries are built and uploaded by goreleaser. Please refer to the configuration file: .goreleaser.yml

Author

moznion ([email protected])

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].