go-sfgen

command module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2025 License: MIT Imports: 18 Imported by: 0

README

go-sfgen

See https://pkg.go.dev/github.com/rad12000/go-sfgen for detailed documentation.

go-sfgen is a command line tool, designed to be used in // go:generate directives. It aims to remove the boilerplate of creating const values that match tag name values. For example:

package main

type Person struct {
	FullName string `db:"full_name"`
	Age     int     `db:"age"`
}

const (
	DBColFullName = "full_name"
	DBColAge      = "age"
)

becomes

// -- main.go --
//go:generate go-sfgen --struct Person --tag db --prefix DBCol --export
package main

type Person struct {
	FullName string `db:"full_name"`
	Age     int     `db:"age"`
}

// -- person_dbcol_generated.go --
const (
	DBColFullName = "full_name"
	DBColAge      = "age"
)

it can also generate new types, type aliases, and generic types for type safety:

Alias
// -- main.go --
//go:generate go-sfgen --style alias --struct Person --tag db prefix DBCol --export
package main

type Person struct {
	FullName string `db:"full_name"`
	Age     int     `db:"age"`
}

// -- person_dbcol_generated.go --
type DBCol = string
const (
	DBColFullName DBCol = "full_name"
	DBColAge      DBCol = "age"
)
Type
// -- main.go --
//go:generate go-sfgen --style typed --struct Person --tag db --prefix DBCol --export
package main

type Person struct {
	FullName string `db:"full_name"`
	Age     int     `db:"age"`
}

// -- person_dbcol_generated.go --
type DBCol string
func (d DBCol) String() string {
	return (string)(d)
}

const (
	DBColFullName DBCol = "full_name"
	DBColAge      DBCol = "age"
)
Generic
// -- main.go --
//go:generate go-sfgen --style typed --struct Person --tag db --prefix DBCol --export
package main

type Person struct {
	FullName string `db:"full_name"`
	Age     int     `db:"age"`
}

// -- person_dbcol_generated.go --
type DBCol[T any] string
func (d DBCol[T]) String() string {
	return (string)(d)
}

const (
	DBColFullName DBCol[string] = "full_name"
	DBColAge      DBCol[int] = "age"
)

One can also generate enum-like values from a struct:

// -- main.go --
//go:generate go-sfgen --style typed --iter --struct Person --export
package main

type Person struct {
	FullName string `db:"full_name"`
	Age     int     `db:"age"`
}

// -- person_dbcol_generated.go --
type Field string
func (f Field) String() string {
	return (string)(f)
}

func (F Field) All() [2]string {
	return [2]string{"FullName", "Age"}
}

const (
	FieldFullName Field = "FullName"
	FieldAge      Field = "Age"
)

Overriding Fields Names

Individual field names can be customized via the sfgen struct tag. The tag's value is written as sfgen:"[field_name][,[tag_name:field_name tag_name:field_name etc...]]. The field_name, if present, will be used as the fieldName in the generated constant, regardless of what we may be generating constants for. If what is essentially a global override does not fit your use case, the field_name may be left empty and followed by a comma. After the comma, any number of space-separated tag_name:field_name key value pairs may be provided. See the examples for more detail.

More Examples

Look at the examples on pkg.go.dev for more detailed examples.

Documentation

Overview

go-sfgen generates constants from struct fields.

Below is a list of flags that can be used with the //go:generate directive.

Usage:

go-sfgen --struct [struct_name] [flags]

Flags are:

-dry-run
	If true, no output file will be written to, but instead results will be written to stdout
-export
	If true, the generated constants will be exported
-gen value
	accepts all the top level flags in a string, allowing multiple generate commands to be specified
-include-struct-name
	If true, the generated constants will be prefixed with the source struct name
-include-unexported-fields
	If true, the generated constants will include fields that are not exported on the struct
-iter
	if true, an All() method will be generated for the type, which returns an array of all the values generated
-out-dir string
	The directory in which to place the generated file. Defaults to the current directory (default ".")
-out-file string
	The file to write generated output to. Defaults to [--struct]_[prefix]_generated.go
-out-pkg string
	The package the generated code should belong to. Defaults to the package containing the go:generate directive
-package string
	The name of the package in which the source struct resides.
-prefix value
	A value to prepend to the generated const names. Defaults to [tag]Field
-src-dir string
	The directory containing the --struct. Defaults to the current directory (default ".")
-struct string
	The struct to use as the source for code generation. REQUIRED
-style string
	Specifies the style of constants desired. Valid options are: alias, typed, generic
-tag string
	If provided, the provided tag will be parsed for each field on the --struct.
	If the tag is missing, the struct field's name is used.
	Otherwise, the first attribute in the tag is used as the name'
-tag-regex string
	This flag requires the --tag flag be provided as well.
	The provided regex will be tested on the specified tag contents for each field.
	The first capture group will be used as the value for the generated constant.
	If the regex does not match the tag contents, the struct field's' name will be used instead.
-tests
	If true, source code in tests will be included. This flag will often need to be used along with the --package flag.

Jump to

Keyboard shortcuts

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