Coder Social home page Coder Social logo

mapop's Introduction

mapop

Regular map[string]interface{} operations. Ruby enumerable inspired package.

Build Status Coverage Status GoDoc Go Report Card BSD License

Methods

  • Keys(input map[string]interface{}) (keys []string)
  • Reject(input map[string]interface{}, keys ...string) map[string]interface{}
  • Select(input map[string]interface{}, keys ...string) map[string]interface{}
  • Split(input map[string]interface{}) (keys []string, values []interface{})
  • Values(input map[string]interface{}) (values []interface{})
  • MapKeys(f func(string) string, input map[string]interface{}) (output map[string]interface{})
  • MapValues(f func(interface{}) interface{}, input map[string]interface{}) (output map[string]interface{})
  • Partition(f func(string, interface{}) bool, input map[string]interface{}) (partition []map[string]interface{})
  • Map(f func(key string, value interface{}) (string, interface{}), input map[string]interface{}) (output map[string]interface{})
  • Collect(input map[string]interface{}) (output map[string]interface{})
  • Merge(maps ...map[string]interface{}) (output map[string]interface{})
  • SelectFunc(f func(key string, value interface{}) bool, input map[string]interface{}) (output map[string]interface{})

Usage

    input :=  map[string]interface{}{
      "Key1": 2,
      "key3": nil,
      "val": 2,
      "val2": "str",
      "val3": 4,
    }

Keys

  keys := mapop.Keys(input)

  > keys["Key1", "key3", "val", "val2", "val3"]

Reject

  input = mapop.Reject(input, "val", "val2", "val3")

  > input{"Key1": 2, "key3": nil}

Select

  input = mapop.Select(input, "val", "val2", "val3")

  > input{"val": 2, "val2": "str", "val3": 4}

Split

  keys, values := mapop.Split(input, "val", "val2", "val3")

  > keys["Key1", "key3", "val", "val2", "val3"]
  > values[2,nil,2,"str",4]

Values

  values := mapop.Values(input)

  > values[2,nil,2,"str",4]

MapKeys

  input = mapop.MapKeys(strins.ToUpper, input)

  > input{"KEY1": 2, "KEY3": nil, "VAL": 2, "VAL2": "str", "VAL3": 4}

MapValues

  input = mapop.MapValues(func(val interface{}) interface{} {
      return "-10"
  }, input)

  > input{"Key1": -10, "key3": -10, "val": -10, "val2": -10, "val3": -10}

Partition

  partitioned := mapop.Partition(func(key string, value interface{}) bool {
    return strings.Contains(key, "val")
  }, input)

  > partitioned[0]{"Key1": 2, "key3": nil}
  > partitioned[1]{"val": 2, "val2": "str", "val3": 4}

Map

  input = mapop.Map(func(key string, value interface{}) (string, interface{}) {
    if strings.Contains(key, "val") {
      return key, key
    } else {
      return key, value
    }
  }, input)

  > input{"Key1": 2, "key3": nil, "val": "val", "val2": "val2", "val3": "val3"}

Collect

  input = mapop.Collect(input)

  > input{"Key1": 2, "val": 2, "val2": "str", "val3": 4}

Merge

  input2 := map[string]interface{}{
    "a2": "str",
    "a3": 4,
  }

  input = mapop.Merge(input, input2)

  > input{"Key1": 2, "key3": nil, "val": 2, "val2": "str", "val3": 4, "a2": "str", "a3": 4}

SelectFunc

  input = mapop.SelectFunc(func(key string, value interface{}) bool {
    return key == "val"
  }, input)

  > input{"val": 2}

License

Copyright (c) 2015, linkosmos All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of mapop nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

mapop's People

Contributors

ernestas-poskus avatar

Stargazers

 avatar 天剑 avatar Liloupar avatar 刘奕聪 avatar Geoffrey Lehner avatar Adithep Narula avatar Matt Ma avatar Janez Troha avatar Viney avatar  avatar flw avatar ypcpy avatar 巴拉巴拉小魔仙 avatar Jerry Lewis avatar Lweo ogeq avatar Sejal avatar Anatoly Bubenkov avatar niten2 avatar a9b avatar David avatar Fredrik Forsmo avatar wppurking avatar Travis Smith avatar Hypnos avatar Fabio Ribeiro avatar Tiago Guedes avatar Rafael Jesus avatar Marcin Bielak avatar Stepan Seliuk avatar Valere JEANTET avatar Radu Topala avatar mkchung avatar Henry avatar Constantine Chuprik avatar Derek W. Stavis avatar Michal Zdrojewski avatar Sahel Jalal avatar HikoQiu avatar bulletRush avatar  avatar Enmanuel Rivera avatar Anil Kumar avatar  avatar  avatar Alexander Staubo avatar Code avatar Matias Insaurralde avatar Michael Boke avatar fourquadrantlogger avatar  avatar

Watchers

James Cloos avatar  avatar

mapop's Issues

The Merge function will not work properly if the map is nested

Try following case and it will fail

package main

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestMergeMap(t *testing.T) {
	t.Run("case1", func(t *testing.T) {
		m1 := map[string]interface{}{
			"a": "1",
		}
		m2 := map[string]interface{}{
			"b": "2",
		}
		got := MergeMap(m1, m2)
		expect := map[string]interface{}{
			"a": "1",
			"b": "2",
		}
		assert.Equal(t, expect, got)
	})
	t.Run("case2", func(t *testing.T) {
		m1 := map[string]interface{}{
			"a": "1",
		}
		m2 := map[string]interface{}{
			"b": map[string]interface{}{
				"c": "2"},
		}
		m3 := map[string]interface{}{
			"b": map[string]interface{}{
				"d": "3"},
		}
		got := MergeMap(m1, m2, m3)
		expect := map[string]interface{}{
			"a": "1",
			"b": map[string]interface{}{
				"c": "2",
				"d": "3",
			},
		}
		assert.Equal(t, expect, got)
	})
}

Good

Can I copy some func from your code?
golangframework/JSON

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.