maptree

package module
v0.0.0-...-41dc051 Latest Latest
Warning

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

Go to latest
Published: May 2, 2020 License: MIT Imports: 1 Imported by: 0

README

maptree

This is a helper library that can be useful if you need to describe data in a tree.

A directory with files to show or any other data that can be structured in that way (documentation, etc).

This is not a radix tree and does not pretend it in any way.

Sample usage:

package main

import (
	"encoding/json"
	"fmt"

	"github.com/fernandezvara/maptree"
)

type metadata struct {
	Owner string `json:"owner"`
}

func main() {

	t := maptree.New()

	t.Set("directory1/file1.txt", metadata{Owner: "user1"}, "this is the text for the file1")
	t.Set("directory1/file2.txt", metadata{Owner: "user1"}, "this is the text for the file2")
	t.Set("directory2/file3.txt", metadata{Owner: "user1"}, "this is the text for the file3")
	t.Set("directory2/file4.txt", metadata{Owner: "user1"}, "this is the text for the file4")
	t.Set("readme.md", metadata{Owner: "user3"}, "this is a readme file")

	b, err := json.MarshalIndent(t.Tree(), "", "  ")
	if err != nil {
		panic(err)
	}

	fmt.Println(string(b))

}

This is the JSON formatted output:

(this can be easily walked on your javascript applications, as example)

{
  "items": {
    "directory1": {
      "items": {
        "file1.txt": {
          "meta": {
            "owner": "user1"
          },
          "data": "this is the text for the file1"
        },
        "file2.txt": {
          "meta": {
            "owner": "user1"
          },
          "data": "this is the text for the file2"
        }
      }
    },
    "directory2": {
      "items": {
        "file3.txt": {
          "meta": {
            "owner": "user1"
          },
          "data": "this is the text for the file3"
        },
        "file4.txt": {
          "meta": {
            "owner": "user1"
          },
          "data": "this is the text for the file4"
        }
      }
    },
    "readme.md": {
      "meta": {
        "owner": "user3"
      },
      "data": "this is a readme file"
    }
  }
}

Documentation

Index

Constants

View Source
const (
	// DefaultSeparator is the string that denotes each part of the path
	DefaultSeparator = "/"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Leaf

type Leaf struct {
	Leafs    map[string]Leaf `json:"items,omitempty"`
	Metadata interface{}     `json:"meta,omitempty"`
	Data     interface{}     `json:"data,omitempty"`
}

Leaf is the unit that contains data and sub leaves

type MapTree

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

MapTree manages data on a map to help returning data hierarchy

func New

func New() *MapTree

New returns an empty MapTree

func (*MapTree) Delete

func (m *MapTree) Delete(key string) bool

Delete the data from a leaf on the tree

func (*MapTree) Separator

func (m *MapTree) Separator(separator string)

Separator allows to change the separator with a custom one

func (*MapTree) Set

func (m *MapTree) Set(key string, metadata, data interface{}) bool

Set adds a new leaf on the tree

func (*MapTree) Tree

func (m *MapTree) Tree() *Leaf

Tree returns the root leaf that contains the tree

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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