go-sfgen

command module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 16 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"
)

Documentation

Overview

go-sfgen generates constants from struct fields.

Below is a list of flags that can be used with the //go:generate directive. Along with those flags, the `sfgen:""` tag may be used to drive behavior for specific fields. `sfgen:"-"` results in skipping the field for code generation. Any other value will be used as the value of the generated constant for that field. E.g. `type Person struct { Name string `sfgen:"name"` }` results in `const fieldName = "name"`

Usage:

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

Flags are:

-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
-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.

Jump to

Keyboard shortcuts

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