Highly configurable struct to map converter.

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. Will convert maps into other maps as well (work in progress).

Features

  • configuration outside the struct
    • could be in a different package - separation of concerns
    • 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
  • ability to omit keys based on field name, value or tag etc.
  • ability to map elements during conversion

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
}
Owner
Jacek Olszak
El Gopher
Jacek Olszak
Similar Resources

Q2entities - Parse the entities string from a Quake 2 .bsp map file. Written in Go

Q2Entities A simple command-line utility to extract the entities string from a Quake 2 map file. Entities? Binary Space Partitioning maps (.bsp) conta

Apr 9, 2022

Goterators - A util library that Supports aggregate & transforms functions Go. Such as filter, map, reduce, find, exist

Goterators - A util library that Supports aggregate & transforms functions Go. Such as filter, map, reduce, find, exist

Goterators Goterators is util library that Supports aggregate & transforms functions Go, including: for-each find exist reduce filter map API and func

Dec 19, 2022

Slice - provides generic Map, Reduce and Filter functions for Go.

slice slice is a simple Go package to provide generic versions of Map, Reduce and Filter on slices. I mainly wrote it as an exercise to get more famil

Jan 1, 2023

Leftright - A concurrent map that is optimized for scenarios where reads are more frequent than writes

leftright A concurrent map that is optimized for scenarios where reads are more

Jan 30, 2022

MapReduceGolang - Map Reduce with Golang

Map Reduce This demonstrates how map reduce can be run for a Synchronous path As

Sep 21, 2022

Timeboundmap - A Map data structure with expiration cleanup

timeboundmap A Map data structure with expiration cleanup Benchmark goos: darwin

Feb 23, 2022

A Runtime Struct Builder for Go

A Runtime Struct Builder for Go

Jul 8, 2022

Robust & Easy to use struct mapper and utility methods for Go

go-model Robust & Easy to use model mapper and utility methods for Go struct. Typical methods increase productivity and make Go development more fun ?

Dec 30, 2022

Go tool to modify struct field tags

Go tool to modify struct field tags

Go tool to modify/update field tags in structs. gomodifytags makes it easy to update, add or delete the tags in a struct field. You can easily add new tags, update existing tags (such as appending a new key, i.e: db, xml, etc..) or remove existing tags

Jan 1, 2023
Comments
  • Add support for map conversion

    Add support for map conversion

    Mapper.MapAny now supports map conversion. map[string]any instances are converted to map[string]interface{}. During conversion usual callbacks are executed: Filter, Rename and MapValue.

  • Add Element.StructField()

    Add Element.StructField()

    This new method returns reflect.StructField if element represents a field of a struct. It can be used for writing callbacks reading field tags for example.

  • Add errors to callbacks

    Add errors to callbacks

    Return error from all callback functions. This way the conversion could be stopped when there is a problem with filtering, renaming or mapping.

    The error returned from callback is then wrapped and returned from Mapper.MapAny method.

  • Add ability to skip conversion

    Add ability to skip conversion

    Is your feature request related to a problem? Please describe. Currently all structs and map[string]any are converted. But there are situations when not all objects should be converted (for example all maps).

    Describe the solution you'd like Provide new callback function ShouldConvert(path, any) (bool, error) which will return true, nil when object should be converted. This function will be called for each struct and map[string]any found during traversing the tree.

    Describe alternatives you've considered Instead of callback a boolean disabling map conversion. But this solution is limited. It will disable all maps, not some of them.

Related tags
COCO (Color Converter) is a color conversion library for Go.

COCO (Color Converter) for Go COCO (Color Converter) is a color conversion library for Go. Heavily inspired by NPM's color-convert. It converts all wa

Oct 4, 2022
Go library for decoding generic map values into native Go structures and vice versa.

mapstructure mapstructure is a Go library for decoding generic map values to structures and vice versa, while providing helpful error handling. This l

Dec 28, 2022
a thread-safe concurrent map for go

concurrent map As explained here and here, the map type in Go doesn't support concurrent reads and writes. concurrent-map provides a high-performance

Jan 8, 2023
💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more.
💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more.

?? Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more. Go 的一些工具函数,格式化,特殊处理,常用信息获取等等

Jan 6, 2023
Fast integer map for uint32-to-uint32
Fast integer map for uint32-to-uint32

Uint32-to-Uint32 Map This repository contains an implementation of uint32-to-uint32 map which is ~20-50% faster than Go standard map for the same type

Sep 21, 2022
A thread-safe concurrent map for go

concurrent map Original repo didn't support go mod and no any tags,so I forkd this repo add go mod support and patch a tag on this repo. No any code c

Dec 7, 2021
read copy update map for golang 1.18+

(R)ead-(C)opy-Update read copy update map for golang 1.18+ How it works This is a simple generic implementation for https://en.wikipedia.org/wiki/Read

Dec 11, 2022
Experimenting with golang generics to implement functional favorites like filter, map, && reduce.

funcy Experimenting with golang generics to implement functional favorites like filter, map, && reduce. 2021-12 To run the tests, you need to install

Dec 29, 2021
Map downloader and configurator for KillingFloor 2

kf2-map-config Copy the kf2-map-config.exe and maps.txt into the Killing Floor2

Jul 2, 2022
Automatically creates & tiles .tmx format maps from a world map interface
Automatically creates & tiles .tmx format maps from a world map interface

Autotile Create tiled maps for an arbitrarily large world space from a simple interface, then add larger objects randomly with simple rules (eg. place

Aug 19, 2022