GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - elgopher/mapify: Highly configurable struct to map converter.

BranchesTags
Open more actions menu

Latest commit

 

History

41 Commits
41 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
_examples
_examples
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
defaults.go
defaults.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
mapify.go
mapify.go
 
 
mapify_test.go
mapify_test.go
 
 

Repository files navigation

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).

About

Highly configurable struct to map converter.

Resources

Readme
MIT license
Activity

Stars

2 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.