Categorygithub.com/insei/fmap/v2
package
2.0.1
Repository: https://github.com/insei/fmap.git
Documentation: pkg.go.dev

# README

codecov build Goreport GoDoc

FMap

FMap is a simple library for working with structs as map of fields. Switch case and reflect based.

Description

FMap creates new map with filed names as key and with fmap.Field implementations. This is unsafe library, be careful while use.

fmap.Field has default and some advanced methods:

  • Default (for interacting with reflect.StructField values)
GetName() string
GetPkgPath() string
GetType() reflect.Type
GetTag() reflect.StructTag
GetOffset() uintptr
GetIndex() []int
GetAnonymous() bool
IsExported() bool
  • Advanced
GetStructPath() string
GetTagPath(tag string, ignoreParentTagMissing bool) string
GetParent() IField
Get(obj any) any
Set(obj any, val any)
GetPtr(obj any) any

where the obj should be not nil pointer to struct.
GetStructPath() string - returns full path to the field into struct.
GetTagPath(tag string, ignoreParentTagMissing bool) string - returns full path to the field into struct but as tag values.
GetParent() IField - returns a parent field, if exist.
Get(obj any) - return expected typed value as interface{}.
Set(obj any, val any) - val expected typed value to set, {}interface can be used.
GetPtr(obj any) any - return expected typed value pointer to struct field as interface{}.

Example

package main

import (
	"time"
	"fmt"

	"github.com/insei/fmap"
)

type City struct {
	Name string `json:"name"`
}

type People struct {
	Name     string
	Age      uint8
	Birthday time.Time
	City City `json:"city"`
}

func main() {
	p := &People{}
	fields := fmap.Get[People]() // or fmap.GetFrom(p)
	fields["Name"].Set(p, "Test")
	fields["Age"].Set(p, uint8(5))
	fields["Birthday"].Set(p, time.Now())
	fields["City.Name"].Set(p, "DefaultCity")
	jsonPath := fields["City.Name"].GetTagPath("json", false) // city.name
	cityField := fields["City.Name"].GetParent()
	cityStruct := cityField.Get(p)
	fmt.Print(*p, jsonPath, cityStruct)
}

More examples in fields_test.go, like slice fields, nested structs, pointers etc.