tagops

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2025 License: MIT Imports: 11 Imported by: 2

README

tagops

Tagops is a Go library containing a set of utilities for transforming struct fields into maps.

License: MIT

Documentation

Overview

Package tagops is aimed to provide a set of functions to work with struct tags.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Keys

func Keys(m map[string]any) []string

Keys returns a sorted list of keys for the map m.

func MapValues

func MapValues(out *[]any, m map[string]any, order []string) (err error)

MapValues populates slice out with values from map m in the key order specified by order. The size of out slice will be adjusted to order size to accomodate for all values.

func PrepareToMap

func PrepareToMap(opts ...Option) func(any) map[string]any

PrepareToMap returns a ToMap function with options set by opts.

func Tags

func Tags(a any, tag string) []string

Tags returns a sorted list of names in tags, given a struct object. The empty fields are included and the map is flattened.

func ToMap

func ToMap(a any, tag string, omitempty bool, flatten bool) map[string]any

ToMap converts an argument a which should be some struct type, to a map[tag]value. If omitempty is specified, fields having empty values and tag option "omitempty" are skipped. If flatten is true, all nested non-anonymous structs are flattened into the parent map.

Example (Anonymous)
// Anonymous structures are always flattened.
type Person struct {
	Name string `json:"name,omitempty"`
	Age  int    `json:"age,omitempty"`
}
type Employee struct {
	Person
	Position string `json:"position,omitempty"`
}

p := Employee{
	Person: Person{
		Name: "Bob",
		Age:  30,
	},
	Position: "Manager",
}

mp := ToMap(p, "json", true, false) // flatten is ignored
printjson(mp)
Output:

{
  "age": 30,
  "name": "Bob",
  "position": "Manager"
}
Example (Flatten)
type Address struct {
	Street string `json:"street,omitempty"`
	City   string `json:"city,omitempty"`
	ZIP    int    `json:"zip"`
}
type Person struct {
	Name    string  `json:"name,omitempty"`
	Age     int     `json:"age,omitempty"`
	Address Address `json:"address,omitempty"`
}

p := Person{
	Name: "Alice",
	Age:  26,
	Address: Address{
		Street: "123 Main St",
		City:   "Anytown",
		ZIP:    0,
	},
}

mp := ToMap(p, "json", true, true)
printjson(mp)
Output:

{
  "age": 26,
  "city": "Anytown",
  "name": "Alice",
  "street": "123 Main St",
  "zip": 0
}
Example (Nested)
type Address struct {
	Street string `json:"street,omitempty"`
	City   string `json:"city,omitempty"`
	ZIP    int    `json:"zip"`
}
type Person struct {
	Name    string  `json:"name,omitempty"`
	Age     int     `json:"age,omitempty"`
	Address Address `json:"address,omitempty"`
}

p := Person{
	Name: "Alice",
	Age:  26,
	Address: Address{
		Street: "123 Main St",
		City:   "Anytown",
		ZIP:    0,
	},
}

mp := ToMap(p, "json", true, false)
printjson(mp)
Output:

{
  "address": {
    "city": "Anytown",
    "street": "123 Main St",
    "zip": 0
  },
  "age": 26,
  "name": "Alice"
}

func Values

func Values(a any, tag string) ([]any, error)

Values returns values for the struct object a, given a tag. The empty fields are included and the map is flattened. The values are returned in the alphabetical order of tags.

Types

type Mapper

type Mapper struct {
	// Tag is the tag name.
	Tag string
	// Omitempty omits empty fields.
	Omitempty bool
	// Flatten flattens named nested structs (anonymous structs are always
	// flattened).
	Flatten bool
}

Mapper is a struct to map struct fields to map key/values. The struct fields are mapped to map keys using the Tag. No tag value leads to undefined behavior. The Mapper can be configured with options.

func New

func New(opts ...Option) Mapper

New returns a new Mapper with options opts.

func (Mapper) Tags

func (m Mapper) Tags(a any) []string

Tags returns a sorted list of names in tags, given a struct object. The empty fields are included and the map is flattened.

func (Mapper) ToMap

func (m Mapper) ToMap(a any) map[string]any

func (Mapper) Values

func (m Mapper) Values(a any) ([]any, error)

Values returns values for the struct object a, given a tag. The empty fields are included and the map is flattened. The values are returned in the alphabetical order of tags.

type Option

type Option func(*Mapper)

Option is a functional option for Mapper.

func Flatten

func Flatten() Option

Flatten returns an Option that sets the Flatten option to true.

func Omitempty

func Omitempty() Option

Omitempty sets the Omitempty option to true.

func Tag

func Tag(tag string) Option

Tag returns an Option that sets the Tag to tag.

Jump to

Keyboard shortcuts

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