encoder

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultCensorFieldTag is a default tag name for censor fields.
	DefaultCensorFieldTag = "censor"

	// UnsupportedTypeTmpl is a template for a value that is returned when a given type is not supported.
	UnsupportedTypeTmpl = "unsupported type="
)

Variables

This section is empty.

Functions

func PrepareTextMarshalerValue

func PrepareTextMarshalerValue(tm encoding.TextMarshaler) string

PrepareTextMarshalerValue marshals a value that implements encoding.TextMarshaler interface to string.

Types

type Config

type Config struct {
	// DisplayMapType is used to display map type in the output.
	// The default value is false.
	DisplayMapType bool `yaml:"display-map-type"`
	// DisplayPointerSymbol is used to display '&' (pointer symbol) in the output.
	// The default value is false.
	DisplayPointerSymbol bool `yaml:"display-pointer-symbol"`
	// DisplayStructName is used to display struct name in the output.
	// A struct name includes the last part of the package path.
	// The default value is false.
	DisplayStructName bool `yaml:"display-struct-name"`
	// ExcludePatterns contains regexp patterns that are used for the selection
	// of strings that must be masked.
	ExcludePatterns []string `yaml:"exclude-patterns"`
	// MaskValue is used to mask struct fields with sensitive data.
	// The default value is stored in DefaultMaskValue constant.
	MaskValue string `yaml:"mask-value"`
	// UseJSONTagName sets whether to use the `json` tag to get the name of the struct field.
	// If no `json` tag is present, the name of the struct field is used.
	UseJSONTagName bool `yaml:"use-json-tag-name"`
}

Config describes censor Encoder configuration.

type Encoder

type Encoder interface {
	Struct(b *strings.Builder, rv reflect.Value)
	Ptr(b *strings.Builder, rv reflect.Value)
	Slice(b *strings.Builder, rv reflect.Value)
	Map(b *strings.Builder, rv reflect.Value)
	Interface(b *strings.Builder, rv reflect.Value)
	String(b *strings.Builder, s string)
	Encode(b *strings.Builder, f reflect.Value)
}

type TextEncoder

type TextEncoder struct {

	// DisplayMapType is used to display map type in the output.
	// The default value is false.
	DisplayMapType bool
	// DisplayPointerSymbol is used to display '&' (pointer symbol) in the output.
	// The default value is false.
	DisplayPointerSymbol bool
	// DisplayStructName is used to display struct name in the output.
	// A struct name includes the last part of the package path.
	// The default value is false.
	DisplayStructName bool
	// contains filtered or unexported fields
}

TextEncoder is a struct that contains options for parsing.

func NewTextEncoder

func NewTextEncoder(c Config) *TextEncoder

NewTextEncoder returns a new instance of TextEncoder with given configuration.

func (*TextEncoder) Encode

func (te *TextEncoder) Encode(b *strings.Builder, f reflect.Value)

func (*TextEncoder) Interface

func (te *TextEncoder) Interface(b *strings.Builder, rv reflect.Value)

Interface parses an interface and returns an Interface. In case of a pointer to unsupported type of value, a string built from UnsupportedTypeTmpl is used instead of the real value. That string contains a type of the value.

func (*TextEncoder) Map

func (te *TextEncoder) Map(b *strings.Builder, rv reflect.Value)

Map parses a given value and returns a Map. If value is a struct/pointer/slice/array/map/interface, it will be parsed recursively. Note: this method panics if the provided value is not a complex.

func (*TextEncoder) Ptr

func (te *TextEncoder) Ptr(b *strings.Builder, rv reflect.Value)

Ptr parses a given value and returns a Ptr. If the value is nil, it returns a Ptr with a nil Value. In case of a pointer to unsupported type of value, a string built from UnsupportedTypeTmpl is used instead of the real value. That string contains a type of the value.

func (*TextEncoder) Slice

func (te *TextEncoder) Slice(b *strings.Builder, rv reflect.Value)

Slice parses a given value and returns a Slice. This function is also can be used to parse an array. All supported complex types will be parsed recursively. Note: this method panics if the provided value is not a complex.

func (*TextEncoder) String

func (te *TextEncoder) String(b *strings.Builder, s string)

String formats a value as a string.

func (*TextEncoder) Struct

func (te *TextEncoder) Struct(b *strings.Builder, rv reflect.Value)

Struct parses a given value and returns a Struct. All supported complex types will be parsed recursively. Note: all unexported fields will be ignored.

Jump to

Keyboard shortcuts

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