mapify

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: MIT Imports: 3 Imported by: 1

README

Mapify

Build Go Reference Go Report Card codecov

Highly configurable struct to map converter. Converts map[string]any into other maps as well.

Features

  • configuration outside the struct
    • could be in a different package
    • no need to modify original structs (by adding tags, implementing methods etc.)
    • behaviour as a code - you provide code which will be run during conversion
  • ability to:
    • rename keys during conversion
    • omit keys based on field name, value or tag etc.
    • map elements during conversion
    • specify which structs should be converted to maps

Installation

# Add mapify to your Go module:
go get github.com/elgopher/mapify        

Hello, world!

package main

import (
	"fmt"

	"github.com/elgopher/mapify"
)

// This example shows how to convert struct into map
func main() {
	s := SomeStruct{Field: "value"}

	// create Mapper instance. Here default parameters are used.
	mapper := mapify.Mapper{} 
	// MapAny maps any object - this can be a struct, slice or map. The whole object is traversed in order to find
	// all nested structs. Each struct will be converted to map[string]interface{}
	result, err := mapper.MapAny(s)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v", result) // map[Field:value]
}

type SomeStruct struct {
	Field string
}

MapAny algorithm

  1. Take an object which is a struct, map or slice.
  2. Traverse entire object looking for nested structs or maps.
  3. Filter elements (struct fields and map keys).
  4. Rename field names or map keys.
  5. Map (struct field or map values).

Documentation

Overview

Package mapify converts structs (and other maps) into maps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {
	reflect.Value
	// contains filtered or unexported fields
}

Element represents either a map entry, field of a struct or unnamed element of a slice.

func (Element) Name

func (e Element) Name() string

Name returns field name of a struct, key of a map or empty string, when it represents element of a slice.

func (Element) StructField added in v0.3.0

func (e Element) StructField() (_ reflect.StructField, ok bool)

StructField returns the reflect.StructField if e represents a field of a struct. If not, ok is false.

type Filter

type Filter func(path string, e Element) (bool, error)

Filter returns true when element should be included. If error is returned then the whole conversion is aborted and wrapped error is returned from Mapper.MapAny method.

type MapValue

type MapValue func(path string, e Element) (interface{}, error)

MapValue maps (transforms) element value. If error is returned then the whole conversion is aborted and wrapped error is returned from Mapper.MapAny method.

type Mapper added in v0.2.0

type Mapper struct {
	ShouldConvert ShouldConvert
	Filter        Filter
	Rename        Rename
	MapValue      MapValue
}

Mapper represents instance of mapper

func (Mapper) MapAny added in v0.2.0

func (i Mapper) MapAny(v interface{}) (interface{}, error)

MapAny maps any object (struct, map, slice etc.) by converting each struct found to a map.

  • for struct the returned type will be map[string]interface{}
  • for slice of structs the returned type will be []map[string]interface{}

type Rename

type Rename func(path string, e Element) (string, error)

Rename renames element name. If error is returned then the whole conversion is aborted and wrapped error is returned from Mapper.MapAny method.

type ShouldConvert added in v0.5.0

type ShouldConvert func(path string, value reflect.Value) (bool, error)

ShouldConvert returns true when value should be converted to map. The value can be a struct, map[string]any or slice.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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