keytree

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MulanPSL-2.0 Imports: 1 Imported by: 0

README

Keytree

Keytree is a library that helps you to transfer flatten key slice to cascade key tree.

Flatten key slice (before transfer):

a
a/           // item and dir with same name
a/dir1/      // empty dir
a/dir2/item3 // item without parent dir
a/item
a/item2
a1/
b/
b/dir3/
b/dir3/item4

Key tree (after transfer):

[
  {
    "name": "a",
    "type": "dir",
    "children": [
      {
        "name": "dir1",
        "type": "dir",
        "isLeaf": false
      },
      {
        "name": "dir2",
        "type": "dir",
        "children": [
          {
            "name": "item3",
            "type": "item",
            "isLeaf": true
          }
        ],
        "isLeaf": false
      },
      {
        "name": "item1",
        "type": "item",
        "isLeaf": true
      },
      {
        "name": "item2",
        "type": "item",
        "isLeaf": true
      }
    ],
    "isLeaf": false
  },
  {
    "name": "a1",
    "type": "dir",
    "isLeaf": false
  },
  {
    "name": "b",
    "type": "dir",
    "children": [
      {
        "name": "dir3",
        "type": "dir",
        "children": [
          {
            "name": "item4",
            "type": "item",
            "isLeaf": true
          }
        ],
        "isLeaf": false
      }
    ],
    "isLeaf": false
  },
  {
    "name": "a",
    "type": "item",
    "isLeaf": true
  }
]

Usage

  1. Transfer []string to KeyMap (unordered but have unique key index)
keyMap := keytree.BuildMap(keys)

Each key with a slash suffix (like a/) represents a "directory".

Keys without parent directory (like a/dir2/item3 above) will be fixed by adding missing "parent".

The JSON form of keyMap is shown below. If you print keyMap, the result may be different in key sequence because map is unordered.

{
  "a": null,
  "a/": {
    "dir1/": {},
    "dir2/": {
      "item3": null
    },
    "item1": null,
    "item2": null
  },
  "a1/": {},
  "b/": {
    "dir3/": {
      "item4": null
    }
  }
}
  1. Transfer KeyMap to KeyList (ordered but have no key index)
keyList := keytree.BuildKeyListFromMap(keyMap)

The JSON form of keyList is shown at the top. It's ordered.

Frontend Friendly

The result KeyList json could be directly passed to the data field of frontend tree components.

Supported frontend tree modules:

  • Vue el-tree
  • Ant Design Tree

Unsupported Keys

If you use this library in Consul or Etcd, note that their keys are NOT strictly cascaded. You should exclude some special keys such as

  • empty string ""
  • slash "/"
  • key with 2 or more continous slash (like "a//b")

License

The license is switched from GPLv3 to MulanPSL-2.0 (v0.1.1 and after), with a supplementary note file to fill the license template.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Key

type Key struct {
	Name     string  `json:"name"`
	Type     string  `json:"type"`
	Children KeyList `json:"children,omitempty"`

	// IsLeaf is used in el-tree rendering.
	// Add other fields if needed.
	IsLeaf bool `json:"isLeaf"`
}

type KeyList

type KeyList []*Key

KeyList is ordered, but does not have unique key index.

func BuildKeyListFromMap

func BuildKeyListFromMap(m KeyMap) KeyList

func (KeyList) Len

func (k KeyList) Len() int

func (KeyList) Less

func (k KeyList) Less(i, j int) bool

Less function sets "dir"s before "item"s

func (KeyList) Swap

func (k KeyList) Swap(i, j int)

func (KeyList) ToStringSlice added in v0.1.2

func (k KeyList) ToStringSlice() []string

type KeyMap

type KeyMap map[string]KeyMap

KeyMap is unordered, but has unique key index.

func BuildMap

func BuildMap(keys []string) KeyMap

Jump to

Keyboard shortcuts

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