stringsizer

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2018 License: MIT Imports: 5 Imported by: 7

README

stringsizer

travis go report card coverage godocs

A very simple way to exchange keys in a map for a shorter version of the key (1-2 chars). Basically it converts {"some-long-key":"data"} into "a":"data". It keeps track of how the map keys are converted so they can be converted back to the original. Note: The resulting data is not JSON (its missing {}) which makes it a little smaller, and also forces you to transform back to use it.

Why?

I plan on encoding the same set of 10-100 MAC addresses in JSON payloads that are each inserted to a row of a database. This way will be a fast and efficient way to store encode the JSON names across every row so that it reduces the size of the keys (from 17 bytes to 1).

Usage

// make a map
someMap := make(map[string]interface{})
someMap["some-long-key"] = "data"

// Create a new string sizer
ss, _ := New()

// create a shortened string JSON version of the map
shortedJSONOfSomeMap := ss.ShrinkMapToString(someMap)
fmt.Println(shortedJSONOfSomeMap)
// "a":"data"

// Store the string sizer to expand it later
saved := ss.Save()
fmt.Println(saved)
// {"encoding":{"some-long-key":"a"},"current":1}

// reload the string sizer
ss2, _ := New(saved)
// reload the original map from shortened string version
originalMap, _ := ss2.ExpandMapFromString(shortedJSONOfSomeMap)
fmt.Println(originalMap)
// map[some-long-key:data]

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Transform added in v1.0.0

func Transform(originalNumber int) (encoded string)

Transform will take a number and convert it to a string that resembles its base formula. Consider the base character library "abc". The number 0 will just be "a". The number 1 will be "b", 2 will be "c", and 3 will be "ba". (The preceding "a" is not shown).

Types

type StringSizer added in v1.0.0

type StringSizer struct {
	Encoding map[string]string `json:"encoding"`
	Current  int               `json:"current"`
	sync.RWMutex
}

StringSizer defines the patterns of encoding and the current state of encoding.

func New added in v1.0.0

func New(savedStringSizer ...string) (ss *StringSizer, err error)

New generates a new map key shrinker. You can optionally pass a previously saved StringSizer that was returned from the `.Save()` function.

func (*StringSizer) ExpandMap added in v1.0.0

func (ss *StringSizer) ExpandMap(m map[string]interface{}) (decoded map[string]interface{}, err error)

ExpandMap will transform back each string key in a map and return the original map subsituted with its original string keys. Returns an error when a key does not exist.

func (*StringSizer) ExpandMapFromString added in v1.0.0

func (ss *StringSizer) ExpandMapFromString(s string) (m map[string]interface{}, err error)

ExpandMapFromString will return a map from the shortened JSON of a shrinked map (i.e. the output of `ShrinkMapToString`). Returns an error when a key does not exist.

func (*StringSizer) ExpandString added in v1.0.0

func (ss *StringSizer) ExpandString(transformed string) (original string, err error)

ExpandString will take a shrunk string and expand it to the original string. Returns an error when a key does not exist.

func (*StringSizer) Save added in v1.0.0

func (ss *StringSizer) Save() string

StringSizer will return the a JSON string that can be used to reinitialize the previous state.

func (*StringSizer) ShrinkMap added in v1.0.0

func (ss *StringSizer) ShrinkMap(m map[string]interface{}) (new map[string]interface{})

ShrinkMap takes a map with string keys and converts each string key to the smallest possible string, iterating on the current in the compressor. It returns a new Go map with the transformed string keys.

func (*StringSizer) ShrinkMapToString added in v1.0.0

func (ss *StringSizer) ShrinkMapToString(m map[string]interface{}) (shortenedJSON string)

ShrinkMapToString takes a map with string keys and converts each string to the smallest possible string, iterating on the current compressor. It returns a shortened JSON string, so that is, the map `{"value_of_pi":3.141}` will be returned as the string `"a":3.141` (the brackets are removed).

func (*StringSizer) ShrinkString added in v1.0.0

func (ss *StringSizer) ShrinkString(original string) (transformed string)

ShrinkString will take a single string and convert it to the smallest possible based on the current iterator.

Jump to

Keyboard shortcuts

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