fmap

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: Apache-2.0 Imports: 2 Imported by: 1

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(reflect.StructField) values. This is unsafe library, be careful while use.

fmap.Field has 3 advanced methods:

Get[T any]() any
Set(obj any, val any)
GetPtr(obj any) any

where the obj should be not nil pointer to struct.
Get[T any]() 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
}

type People struct {
	Name     string
	Age      uint8
	Birthday time.Time
	City 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")
	fmt.Print(*p)
}

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any]() map[string]Field

Get returns a map of Field objects. It takes a parameter `T` of type `any`, representing the type to be used for Fields map creation.

func GetFrom

func GetFrom(obj interface{}) map[string]Field

GetFrom returns a map of Field objects. It takes a parameter `obj` of type `interface{}` representing the object to be analyzed. The function first checks if the `obj` type is already in the cache, and if it exists, it returns the cached value. Otherwise, it creates a new empty map with fields.

Types

type Field

type Field reflect.StructField

func (Field) Get

func (f Field) Get(obj interface{}) interface{}

Get returns the value of the fields in the provided object. It takes a parameter `obj` of type `interface{}`, representing the object. It returns the value of the fields as an `interface{}`.

func (Field) GetPtr

func (f Field) GetPtr(obj interface{}) interface{}

func (Field) Set

func (f Field) Set(obj interface{}, val interface{})

Set updates the value of the fields in the provided object with the provided value. It takes two parameters:

  • obj: interface{}, representing the object containing the field.
  • val: interface{}, representing the new value for the field.

The Set method uses the getPtr method to get a pointer to the fields in the object. It then performs a type switch on the kind of the fields to determine its type, and sets the value accordingly. The supported fields types are string, int, and bool. If the fields type is not one of the supported types, it panics with the message "unhandled default case".

Jump to

Keyboard shortcuts

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