orderedmap

package module
v0.0.0-...-1de5b7d Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2018 License: MIT Imports: 3 Imported by: 0

README

orderedmap

Orderedmap works like a map[string]interface{} but preserves the order in which keys have been added. It has been optimized performance-wise while still keeping memory usage at a minimum. All operations have O(1) time complexity.

Orderedmap is useful for generating human-readable JSON and can be easily marshalled using golang's encoding/json package.

Full example code

package main

import "fmt"
import "encoding/json"

import om "github.com/maurice2k/orderedmap"

func main() {
    status := om.NewOrderedMap(  // initializes a new odered map
        &om.KV{"http", om.NewOrderedMap(  // key-value with sub ordered map as value
            &om.KV{"code", 200},  // simple key-value
            &om.KV{"message", "OK"},
        )})

    response := om.NewOrderedMap().  // now with Setter
        Set("success", true).
        Set("status", status)

    contentResponse := om.NewOrderedMap(
        &om.KV{"user", om.NewOrderedMap(
            &om.KV{"id", 1337},
            &om.KV{"name", "John Doe"},
        )})

    contentResponse.Set("success", false)  // will be skipped in Append(..., false) call

    // append content to response without overwriting existing keys
    // (second parameter set to false)
    response.Append(contentResponse, false)

    jsonData, _ := json.MarshalIndent(response, "", "  ")
    fmt.Println(string(jsonData))
}

Output

{
  "success": true,
  "status": {
    "http": {
      "code": 200,
      "message": "OK"
    }
  },
  "user": {
    "id": 1337,
    "name": "John Doe"
  }
}

Documentation

Overview

Copyright 2017 Moritz Fain Moritz Fain <moritz@mertinkat.net>

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KV

type KV struct {
	Key   KeyType
	Value ValueType
}

func (KV) MarshalJSON

func (kv KV) MarshalJSON() ([]byte, error)

Marshal JSON for single KV item (convenience only)

func (KV) MarshalYAML

func (kv KV) MarshalYAML() ([]byte, error)

MarshalYAML Marshals the ordered map to yaml

type KeyType

type KeyType string

type OrderedMap

type OrderedMap struct {
	// contains filtered or unexported fields
}

func NewOrderedMap

func NewOrderedMap(kvList ...*KV) (om *OrderedMap)

Creates new ordered map

func (*OrderedMap) Append

func (om *OrderedMap) Append(newOm *OrderedMap, overwrite bool) *OrderedMap

Appends the given ordered map to the current one

func (*OrderedMap) Delete

func (obj *OrderedMap) Delete(key KeyType)

func (*OrderedMap) Exists

func (om *OrderedMap) Exists(key KeyType) (ok bool)

Checks for existence of a given key

func (*OrderedMap) Get

func (om *OrderedMap) Get(key KeyType) ValueType

Returns the given key's value or <nil> if key does not exist

func (*OrderedMap) GetKeys

func (om *OrderedMap) GetKeys() (keys []KeyType)

Returns ordered list of keys

func (*OrderedMap) GetList

func (om *OrderedMap) GetList() (kvList []KV)

Returns ordered list of key-value pairs

func (*OrderedMap) Len

func (om *OrderedMap) Len() int

Returns length

func (*OrderedMap) MarshalJSON

func (om *OrderedMap) MarshalJSON() ([]byte, error)

Marshal JSON for ordered map

func (*OrderedMap) MarshalYAML

func (om *OrderedMap) MarshalYAML() ([]byte, error)

MarshalYAML Marshalls the ordered map to yaml

func (*OrderedMap) Set

func (om *OrderedMap) Set(key KeyType, value ValueType) *OrderedMap

Sets value for given key

func (*OrderedMap) String

func (om *OrderedMap) String() string

Returns JSON serialized string representation

type ValueType

type ValueType interface{}

Jump to

Keyboard shortcuts

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