Skip to content

Latest commit

 

History

History
68 lines (52 loc) · 2.25 KB

README.md

File metadata and controls

68 lines (52 loc) · 2.25 KB

Mapify

Project Status: Active – The project has reached a stable, usable state and is being actively developed. 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).