go-stringer

command module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: BSD-3-Clause Imports: 17 Imported by: 0

README

Aboud go-stringer

Fork of https://github.com/golang/tools/blob/master/cmd/stringer/stringer.go

Forked on commit: 28cd7188fd2c8f3ede85d4b7a6734cc426422094 ref Unfortunately, I don't know how to fork only cmd/stringer, so there is no direct connection and it seems impossible to synchronize changes automatically.

Motivation

To extend the generator functional to get automatically the text in the format I need and an additional function that returns enum value by text.

Changes from original

  • Subjective improvements
    • For my comfort, the monolithic stringer.go is divided into component files
    • Added samples for each generator builder case
    • Added flag target to make it easier to run debug on samples
  • Improved file naming of output files in case no special name defined
    • Added flag outputtransform to convert strings of file names in similar way as names example: originally it generate type PatternRuneWithRange by default as 'patternrunewithrange_string.go' but i want to get it as 'pattern_rune_with_range_string.go'
  • Improved string content generation
    • Added flag nametransform to convert strings from the original name to the one I need using github.com/gobeam/stringy my pet project required a snake case and I didn't want to do it by hand
  • Added new optional stage to generate function go parse enum from string
    • Added flag fromstringgenfn to activate generation of ${TypeName}FromString() functions
  • Added new optional stage to generate extra consts in format {Prefix}{PackageName}{TypeValue}{Suffix}, to active it required to define one of activation flag below
    • Added flag extraconstsnamesuffix to append custom text to the end of string id (first of activation flag)
    • Added flag extraconstsnameprefix to append custom text before the const name (second of activation flag)
    • Added flag extraconstsvaluetransform convert string id format similar to nametransform (optional)
    • Added flag extraconstsvaluesuffix to append some text to each const value (optional)
  • Added new optional stage to generate marshalling and unmarshalling functions for specific packages
    • Added flag marshaljson to append implementations of json.Marshaler and json.Unmarshaler
    • Added flag marshaljsonpkg to specify url to json package if using forked version or similar
    • Added flag marshalqs to append implementations of qs.MarshalQS and qs.UnmarshalQS
    • Added flag marshalqspkg to specify url to qs package if using forked version or similar

How to use

Install:

go install github.com/dmji/go-stringer@latest

Information about flags can be query with execution without parameters:

go-stringer

Sample of generage comment:

package main

//go:generate go-stringer -type=SimpleRune -trimprefix=SimpleRune -linecomment=true -nametransform=snake_case_lower -outputtransform=snake_case_lower -extraconstsnamesuffix=_i18n_ID -extraconstsvaluetransform=snake_case_lower -extraconstsvaluesuffix=Presentation

type SimpleRune int16

const (
	SimpleRuneFirst SimpleRune = iota
	SimpleRuneSecond
	SimpleRuneThird
	SimpleRuneFourth
)

Documentation

Overview

Stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer interface. Given the name of a (signed or unsigned) integer type T that has constants defined, stringer will create a new self-contained Go source file implementing

func (t T) String() string

The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.

Stringer works best with constants that are consecutive values such as created using iota, but creates good code regardless. In the future it might also provide custom support for constant sets that are bit patterns.

For example, given this snippet,

package painkiller

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

running this command

stringer -type=Pill

in the same directory will create the file pill_string.go, in package painkiller, containing a definition of

func (Pill) String() string

That method will translate the value of a Pill constant to the string representation of the respective constant name, so that the call fmt.Print(painkiller.Aspirin) will print the string "Aspirin".

Typically this process would be run using go generate, like this:

//go:generate stringer -type=Pill

If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_string.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.

Types can also be declared in tests, in which case type declarations in the non-test package or its test variant are preferred over types defined in the package with suffix "_test". The default output file for type declarations in tests is t_string_test.go with t picked as above.

The -linecomment flag tells stringer to generate the text of any line comment, trimmed of leading spaces, instead of the constant name. For instance, if the constants above had a Pill prefix, one could write

PillAspirin // Aspirin

to suppress it in the output.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 daimonji. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 daimonji. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 daimonji. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 daimonji. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Copyright 2014 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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