mapmap

package module
v0.0.0-...-5cf84b8 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2016 License: MIT Imports: 4 Imported by: 0

README

Mapmap GoDoc

It maps your maps!

WARNING: Development of this library has been suspended (3/22/2016) because other libraries seem to provide similar features in a better way. The flatmap package may still be useful for some projects.

WORK IN PROGRESS: Very limited features so far.

Quick Start

Mapmap is for doing quick transformations on map[string]interface{} and []interface{} objects in golang (the stuff you get back when parsing json).

Basic usage for shuffling around fields is as follows:

package main

import (
    "fmt"

    "github.com/turtlemonvh/mapmap"
)

func main() {

    // You need an object to map
    m := map[string]interface{}{
        "cat":        "garfield",
        "dog":        "odie",
        "friends":    []interface{}{"John"},
        "turtle":     "0",
        "57":         int64(57),
        "doesItWork": true,
    }

    // Create a slice of `Mapper`s describing transformations to run
    var mappers []*mapmap.Mapper
    mappers = append(mappers, mapmap.NewMapper("cat", "cat"))
    mappers = append(mappers, mapmap.NewMapper("turtle", "frog"))
    mappers = append(mappers, mapmap.NewMapper("friends.[0]", "myOnlyFriend"))

    // Run the tranformations
    new_map, processingErrors, err = mapmap.MapIt(m, mappers)

    fmt.Println(new_map)
}

This would result in the following value for new_map:

map[string]interface{}{
    "cat":          "garfield",
    "frog":         "0",
    "myOnlyFriend": "John",
}

Testing

go test ./...

Credits

  • Built with the awesome cast library from spf13.
  • Inspired by the viper library from spf13.
  • Lots of source taken from the flatmap library in terraform.
    • Mostly I just manage maps with interfaces and return errors instead of panicing.

Alternatives

The following is a list of libraries that you may want to use instead of mapmap.

TODO

  • add transform functions
  • add validators
  • add type handling
  • add error handling (returning array of errors) in addition to standard handling
  • add benchmarks for simple transformations

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapIt

func MapIt(inMap interface{}, mappers []*Mapper) (interface{}, []error, error)

Types

type Mapper

type Mapper struct {
	InputPath  string // dot delimited path to input field in map
	OutputPath string // dot delimited path to output field in map
	// contains filtered or unexported fields
}

func NewMapper

func NewMapper(input string, output string) *Mapper

Directories

Path Synopsis
Based off https://github.com/hashicorp/terraform/blob/master/flatmap/expand.go
Based off https://github.com/hashicorp/terraform/blob/master/flatmap/expand.go

Jump to

Keyboard shortcuts

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