codemsg

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Copyright 2021-2023 guonaihong. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2021-2023 guonaihong. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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.

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CodeMsg

type CodeMsg struct {
	TypeNames         string `clop:"--type" usage:"comma-separated list of type names; must be set" valid:"required"`
	Output            string `clop:"long" usage:"output file name; default srcdir/<type>_string.go"`
	Trimprefix        string `clop:"long" usage:"trim the 'prefix' from the generated constant names"`
	Linecomment       bool   `clop:"long" usage:"use line comment text as printed text when present"`
	BuildTags         string `clop:"--tags" usage:"comma-separated list of build tags to apply"`
	CodeMsg           bool   `clop:"long" usage:"turn on the ability to generate codemsg code"`
	Grpc              bool   `clop:"long" usage:"Generate grpc error type"`
	StringMethod      string `clop:"long" usage:"String function name" default:"String"`
	String            bool   `clop:"long" usage:"Generate string function"`
	CodeMsgStructName string `clop:"long" usage:"turn on the ability to generate codemsg code" default:"CodeMsg"` // TODO

	CodeName      string   `clop:"long" usage:"set new code name" default:"Code"`
	MsgName       string   `clop:"long" usage:"set new message name" default:"Message"`
	TakeCodeToMap bool     `clop:"long" usage:"Enable the take to map feature"`
	Args          []string `clop:"args=file" usage:"file or dir" default:"["."]"`
	SaveCodeToMap map[string]map[int]bool
}

func (*CodeMsg) SubMain

func (c *CodeMsg) SubMain()

type CodeMsgTmpl

type CodeMsgTmpl struct {
	StringMethod      string // String方法的名字,默认String
	PkgName           string // 包名
	TypeName          string
	CodeMsgStructName string // CodeMsg{Code int, Message string} 结构体的名字
	CodeName          string // 修改Code字段的名字
	MsgName           string // 修改Message 字段的名字
	MsgTagName        string
	CodeTagName       string
	Args              string // os.Args[2:]
	OriginalName      string //
	AllVariable       []Value
}

自定义

func (*CodeMsgTmpl) Gen

func (c *CodeMsgTmpl) Gen(w io.Writer) error

type File

type File struct {
	// contains filtered or unexported fields
}

File holds a single parsed file and associated data.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator holds the state of the analysis. Primarily used to buffer the output for format.Source.

func (*Generator) Printf

func (g *Generator) Printf(format string, args ...interface{})

type GrpcCodeMsgTmpl added in v0.0.8

type GrpcCodeMsgTmpl struct {
	PkgName      string // 包名
	TypeName     string
	MsgName      string // 修改Message 字段的名字
	Args         string // os.Args[2:]
	OriginalName string //
	StringMethod string // String函数名
	AllVariable  []Value
}

自定义

func (*GrpcCodeMsgTmpl) Gen added in v0.0.8

func (c *GrpcCodeMsgTmpl) Gen(w io.Writer) error

type Package

type Package struct {
	// contains filtered or unexported fields
}

type Value

type Value struct {
	OriginalName string // The name of the constant.
	Name         string // The name with trimmed prefix.
	// contains filtered or unexported fields
}

Value represents a declared constant.

func (*Value) String

func (v *Value) String() string

Jump to

Keyboard shortcuts

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