Package figtree provides a multi-paradigm SDK for sophisticated configuration file access

Package figtree provides a multi-paradigm SDK for sophisticated configuration file access.

Motivation

Figtree syntax is based on classic key/value pairs that may be organized into a nested hierarchy of named sections.

Many of the design goals for the figtree syntax come from its predecessors including XML, JSON, YAML, TOML, win.ini, and Apache. The deficiencies that figtree syntax addresses comprise:

  1. XML is verbose, requiring opening and closing tags that obscure the data.
  2. JSON does not allow comments, forcing usage documentation to be written in a separate place.
  3. YAML uses indentation to indicate hierarchical structure, which is not as appealing to some users as curly braces.
  4. TOML, JSON and YAML require excessive use of quotation-marks, colons, equal-signs, hyphens, or commas purely for the sake of the parsing algorithm.
  5. TOML, JSON and YAML require special notation to declare multi-value arrays.
  6. Some configuration file editors arbitrarily re-sort keys, loosing the original author's intended ordering.
  7. Some configuration file editors arbitrarily drop comments from file.

The features that figtree syntax incorporates comprise:

  1. Like TOML and YAML, keynames do not need to be delimited by quotation-marks.
  2. Like YAML, values do not need to be delimited by quotation-marks.
  3. Like XML's XPath, the configuration hierarchy can be accessed using keyPath notation.
  4. Like XML, the configuration can be validated against a document type definition.
  5. Like win.ini, and unlike the Windows system registry, the configuration is held in plain text files.
  6. Like Apache configuration files, portions of the configuration can be included from separate files.
  7. Like many Linux-based tools, default baseline settings can be kept separate from user overrides.

Figtree syntax

There are only a few essential constructs to understand. First, simple key/value pairs appear on a single line. Unlike other configuration syntax styles, key/value pairs do not use colons or equal-signs as assignment operators. Instead, the presence of whitespace between the key-name and the beginning of the value is enough to parse the line into left-hand and right-hand halves.

Furthermore, values are not delimited by quotation marks. Instead, all leading and trailing whitespace characters are stripped from the right-hand side of the key/value pair to produce the value. Examples:

 hostname    figtree
 url         https:www.figtree.io
 version     1.0
 note        A fig tree should not be confused with a "figtree"

The second construct to understand are named sections, which are multi-line collections of key/value pairs. Named sections have a key name followed by a K&R-style pair of curly braces.

The opening brace must be the last non-whitespace character of a line. The closing brace must be the first non-whitespace character of a line. The lines between the opening and closing braces may contain both simple key/value pairs and other named sections. Sections may be nested arbitrarily deep. Example:

 ip-settings {
     ip4 {
         inet       179.100.102.215
         netmask    255.255.240.0
         broadcast  167.111.255
         gateway    167.99.96.0
     }
     ip6 {
         inet       1fe80::e457:b8ff:fe48:8ead
     }
 }

Duplicate, non-unique keys are used to declare array-like values. Any key that appears more than once per section is considered to be an array. The ItemIsArray function can be used to test whether or not a key has multiple values. The QueryAll function can be used to obtain multiple items with the same key. Example:

 name-servers {
     ns ns1.figtree.net
     ns ns2.figtree.net
     ns ns3.figtree.net
 }

Block comments are written using hashtags as the first non-whitespace character of a line. Example:

 # All ip-settings are required

Terminal comments may appear on the same line together with key/value pairs when whitespace and a hash tag follow the value. Example:

 hostname    figtree                   # the short device name
 url         https:www.figtree.io      # protocol, optional port, and DNS name
 version     1.0                       # minimum required version
 reference   allthedocs.org#figtree    # a hashtag embedded within the value (with no preceding space)

Reading and writing

Configuration files written in figtree syntax can be read into memory using the ReadConfig function. Example:

configFilename := "testdata/fixtures/sample"
 root, err := ReadConfig(configFilename)
 if err != nil {
     return
 }
 # root is a pointer to the in-memory hierarchical tree

An in-memory tree can be saved using any type that implements the SerializeBranch function, which is called by WriteToFile and WriteToBuffer. Example:

 writer := FigtreeWriter{}
 configFilename := "testdata/fixtures/sample"
 err := root.WriteToFile(writer, configFilename)
 if err != nil {
     t.Errorf(err.Error())
 }

Accessing figtree items

Programs may access individual items using either simpleKeyNames or keyPaths. A simpleKeyName is a key as it appears in the figtree file. A keyPath is a slash-separated sequence of key names where each segment matches successively deeper sections of the hierarchy. Examples:

 var item *Item
 var err error
 item, err = QueryOne("hostname")               simpleKeyName
 item, err = QueryOne("ip-settings/ip4/inet")   keyPath

Arrays can be accessed similarly. Example:

 var items []Items
 items = QueryAll("name-servers/ns")

An item's value is an anonymous interface which may be a string value or a Branch pointer. When a program knows the type of item that it is looking for it may be simpler to use either the GetValue or GetBranch function to directly access it. When unsure, a program may use GetItem at the cost of having to explicitly work around the ambiguity.

Testing for the existence of a simpleKeyName is done with ItemExists. Testing for the existence of a keyPath is done with PathExists. Checking to see if a key has multiple values is done with ItemIsArray.

Manipulating the figtree

Figtree items and branches can be programmatically manipulated. Adding new keys is done with AppendItem, PrependItem, InsertBeforeItem, or InsertAfterItem. Removing keys is done with RemoveItem.

Owner
Read Write Pro
Open source Golang packages
Read Write Pro
Similar Resources

Go package providing opinionated tools and methods for working with the `aws-sdk-go/service/cloudfront` package.

go-aws-cloudfront Go package providing opinionated tools and methods for working with the aws-sdk-go/service/cloudfront package. Documentation Tools $

Feb 2, 2022

The MinIO Admin Go Client SDK provides APIs to manage MinIO services

Golang Admin Client API Reference The MinIO Admin Golang Client SDK provides APIs to manage MinIO services. This quickstart guide will show you how to

Dec 30, 2022

Metrics package helps to create ydb-go-sdk traces with monitoring internal state of driver

metrics metrics package helps to create ydb-go-sdk traces with monitoring internal state of driver Usage import ( "fmt" "sync/mutex" "time

Jan 7, 2023

A SDK development package developed in go language and connected to dechain

Platform introduction Dechain go SDK is a SDK development package developed in go language and connected to dechain. This SDK can be provided to go cl

Dec 14, 2021

Package captcha is a middleware that provides captcha service for Flamego

auth Package captcha is a middleware that provides captcha service for Flamego. Installation The minimum requirement of Go is 1.16. go get github.com/

Dec 14, 2022

Utilcanvas is a package which provides some utilities to interface with the Canvas LMS Api.

Utilcanvas Utilcanvas is a package which provides some utilities to interface with the Canvas LMS Api. Example usage c := utilcanvas.NewClient("https:

Dec 21, 2022

This package provides a Go interface for the Typeform API.

typeform This package provides a Go interface for the Typeform API

Dec 4, 2022

It is a package and command line application that provides you to store encrypted credentials/secrets in your repository.

sypher[ ⚠️ Work in progress] sypher provides you to store your credentials and secrets as encrypted in your repository. Usage Install the command line

Feb 23, 2022
rpCheckup is an AWS resource policy security checkup tool that identifies public, external account access, intra-org account access, and private resources.
rpCheckup is an AWS resource policy security checkup tool that identifies public, external account access, intra-org     account access, and private resources.

rpCheckup - Catch AWS resource policy backdoors like Endgame rpCheckup is an AWS resource policy security checkup tool that identifies public, externa

Dec 26, 2022
A cloud native Identity & Access Proxy / API (IAP) and Access Control Decision API

Heimdall Heimdall is inspired by Ory's OAthkeeper, tries however to resolve the functional limitations of that product by also building on a more mode

Jan 6, 2023
Package anko provides a golang SDK to the Anko Investor Forecasts gRPC service.

github.com/anglo-korean/anko-go-sdk Package anko provides a golang SDK to the Anko Investor Forecasts gRPC service. import "github.com/anglo-korean/an

Jan 3, 2022
SDK to provide access to JUNO API (Open Banking) (2.0.0)

Juno API - Golang SDK Juno API (Open Banking) (2.0.0) Why? This project is part of my personal portfolio, so, I'll be happy if you could provide me an

Aug 9, 2021
Unofficial SDK to access for Open Threat Exchange (OTX) in Go

gotx Unofficial SDK to access for Open Threat Exchange (OTX) API in Go. Usage package main import ( "context" "fmt" "os" "github.com/m-mizutani/

Feb 12, 2022
A go sdk for baidu netdisk open platform 百度网盘开放平台 Go SDK

Pan Go Sdk 该代码库为百度网盘开放平台Go语言的SDK

Nov 22, 2022
Nextengine-sdk-go: the NextEngine SDK for the Go programming language

NextEngine SDK for Go nextengine-sdk-go is the NextEngine SDK for the Go programming language. Getting Started Install go get github.com/takaaki-s/nex

Dec 7, 2021
Commercetools-go-sdk is fork of original commercetools-go-sdk

commercetools-go-sdk The Commercetools Go SDK is automatically generated based on the official API specifications of Commercetools. It should therefor

Dec 13, 2021
Sdk-go - Go version of the Synapse SDK

synapsesdk-go Synapse Protocol's Go SDK. Currently in super duper alpha, do not

Jan 7, 2022
Redash-go-sdk - An SDK for the programmatic management of Redash, in Go
Redash-go-sdk - An SDK for the programmatic management of Redash, in Go

Redash Go SDK An SDK for the programmatic management of Redash. The main compone

Dec 13, 2022