Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/shawnohare/go-record

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/shawnohare/go-record

  • v0.0.0-20150424172904-3071198866fe
  • Source
  • Go
  • Socket score

Version published
Created
Source

go-record

A lightweight Record structure that simplifies access to values in composite (nested) maps.

Usage Examples

The code for these examples is in the examples dir. We first import the package and define a function that records a sample raw record such as might be returned by a database SDK.

package main

import (
	"fmt"

	"github.com/shawnohare/go-record"
)

// Make a new composite map example.
func makeExample() map[string]interface{} {
	var example = map[string]interface{}{
		"1": map[string]interface{}{
			"1": 11,
			"2": 12,
		},
		"2": map[string]interface{}{
			"1": 21,
			"2": 22,
		},
		"3": map[string]interface{}{
			"1": map[string]interface{}{
				"1": map[string]interface{}{
					"1": "value",
				},
			},
		},
	}
	return example
}

New

To create a new empty record, use New.

	var r *record.Record
	r = record.New()

Init

We can also initialize a Record from an existing composite map.

   cmap := makeExample()
   r = record.Init(cmap)

Set and Get

We can insert a value into the record with Set by passing the desired path to the value. A path is always a string of keys separated by a period.

	r.Set("1.3", 13)

The value 13 now resides as a value in a subsubmap of the record. We can extract it with Get, although we must assert types. The second return value is a boolean indicated whether the path exists.

	x, prs := r.Get("1.3")
	if prs {
		x.(int) == 13 // true
	}

If we wish to obtain a nested map instead of a leaf, we just pass the appropriate path.

	subMap, _ := r.Get("1")
	fmt.Println("A nested map:", subMap)

AsMap

AsMap allows access to the underlying composite map.

	var d map[string]interface{}
	d = r.AsMap()
	fmt.Println("The underlying composite map:", d)

Filter

Similarly, we can retrieve a filtered version of the record by passing the desired paths. If we pass a path to a non-leaf node, we obtain all values below the node as well. Filter silently ignores non-existent paths.

	paths := []string{"1.1", "3.1", "badPath"}
	var filtered *record.Record

	filtered = r.Filter(paths)

	fmt.Println("Filtered record:", filtered)
	_, prs = filtered.Get("3.1.1.1") // Access the leaf as expected.
	fmt.Println("Value exists:", prs)
}

FilterMap

FilterMap is a convenience function that performs filtering on raw composite maps rather than Record objects. It's just an exported wrapper for the internal filter function.

  var filteredMap map[string]interface{}
  filteredMap = record.FilterMap(makeExample(), paths)
  fmt.Println("Filtered map:", filteredMap)
  
  // Equivalent to: 
  
  filteredMap = record.Init(makeExample()).Filter(paths).AsMap()
  fmt.Println("Filtered map:", filteredMap)

FAQs

Package last updated on 24 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc