maps

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: MIT Imports: 12 Imported by: 0

README

maps

CI

provides some useful functions for working with maps.

Usage

package maps_test

import (
	"fmt"

	"github.com/sraphs/maps"
)

func Example() {
	m := map[string]interface{}{
		"a": map[string]interface{}{
			"b": map[string]interface{}{
				"nil":          nil,
				"bool":         true,
				"int_key":      1000,
				"float_key":    1000.1,
				"string_key":   "string_value",
				"duration_key": 10000,
			},
		},
	}

	n := maps.Get(m, "a.b.nil").Load()
	b, _ := maps.Get(m, "a.b.bool").Bool()
	i, _ := maps.Get(m, "a.b.int_key").Int()
	f, _ := maps.Get(m, "a.b.float_key").Float()
	s, _ := maps.Get(m, "a.b.string_key").String()
	d, _ := maps.Get(m, "a.b.duration_key").Duration()

	fmt.Println(n)
	fmt.Println(b)
	fmt.Println(i)
	fmt.Println(f)
	fmt.Println(s)
	fmt.Println(d)

	//Output:
	// <nil>
	// true
	// 1000
	// 1000.1
	// string_value
	// 10µs
}

Contributing

We alway welcome your contributions 👏

  1. Fork the repository
  2. Create Feat_xxx branch
  3. Commit your code
  4. Create Pull Request

CHANGELOG

See Releases

License

MIT © sraph.com

Documentation

Overview

package maps provides some useful functions for working with maps.

Example
package main

import (
	"fmt"

	"github.com/sraphs/maps"
)

func main() {
	m := map[string]interface{}{
		"a": map[string]interface{}{
			"b": map[string]interface{}{
				"nil":          nil,
				"bool":         true,
				"int_key":      1000,
				"float_key":    1000.1,
				"string_key":   "string_value",
				"duration_key": 10000,
			},
		},
	}

	n := maps.Get(m, "a.b.nil").Load()
	b, _ := maps.Get(m, "a.b.bool").Bool()
	i, _ := maps.Get(m, "a.b.int_key").Int()
	f, _ := maps.Get(m, "a.b.float_key").Float()
	s, _ := maps.Get(m, "a.b.string_key").String()
	d, _ := maps.Get(m, "a.b.duration_key").Duration()

	fmt.Println(n)
	fmt.Println(b)
	fmt.Println(i)
	fmt.Println(f)
	fmt.Println(s)
	fmt.Println(d)

}
Output:

<nil>
true
1000
1000.1
string_value
10µs

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is key not found.
	ErrNotFound = errors.New("key not found")
)

Functions

func Clone

func Clone(m map[string]interface{}) (map[string]interface{}, error)

Clone performs a deep copy of the given map m.

Types

type Value

type Value interface {
	Bool() (bool, error)
	Int() (int64, error)
	Float() (float64, error)
	String() (string, error)
	Duration() (time.Duration, error)
	Slice() ([]Value, error)
	Map() (map[string]Value, error)
	Scan(interface{}) error
	Load() interface{}
	Store(interface{})
}

Value is config value interface.

func Get

func Get(values map[string]interface{}, path string) Value

Get read Value in given map[string]interface{} by the given path, will return false if not found.

Jump to

Keyboard shortcuts

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