dora

command module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2020 License: MIT Imports: 2 Imported by: 0

README

Dora backpack with JSON

Welcome to dora the JSON explorer 👋

Using go version 1.14 Using go version 1.14 Go Report Card godoc

Dora makes exploring JSON fast, painless, and elegant.

NOTE:

  • dora is currently an early WIP and the main focus was for teaching the content through a medium blog post. In other words, dora is still a ways out from being a stable tool.
  • Recently made an initial pre-release at 0.1.0. New github project tracks progress towards 0.2.0 - a mininum usable API. Releases from 0.1.0 up to but not including 0.2.0 will be marked as pre-releases.

Install

go get github.com/bradford-hamilton/dora/pkg/dora

Usage

var exampleJSON = `{ "string": "a neat string", "bool": true, "PI": 3.14159 }`

c, err := dora.NewFromString(exampleJSON)
if err != nil {
  fmt.Printf("\nError creating client: %v\n", err)
}

str, err := c.GetString("$.string")
if err != nil {
  fmt.Println(err)
}

boolean, err := c.GetBool("$.bool")
if err != nil {
  fmt.Println(err)
}

float, err := c.GetFloat64("$.PI")
if err != nil {
  fmt.Println(err)
}

fmt.Println(str)     // a neat string
fmt.Println(boolean) // true
fmt.Println(float)   // 3.14159

Query Syntax

  1. All queries start with $.

  2. Access objects with . only, no support for object access with bracket notation [].

    • This is intentional, as you can interpolate at the call site, so there is no reason to offer two syntaxes that do the same thing.
  3. Access arrays by index with bracket notation [].

  4. New: Fetch by type to allow caller to ask for the proper Go type. For the time being, asking for objects or arrays in their entirety must be done through GetString which will return the chunk of JSON.

    Current API:

    • GetString
    • GetFloat64
    • GetBool
  5. Next feature will be approaching this either with some sort of serialization option maybe similar to stdlib or a simpler one with no options that returns a map or something? Will think about that some.

Example with a JSON object as root value:

JSON:

{
  "name": "bradford",
  "someArray": ["some", "values"]
  "obj": {
    "innerKey": {
      "innerKey2": "innerValue",
      "innerKey3": [{ "kindOfStuff": "neatStuff" }]
    }
  },
  "someBool": true,
  "PI": 3.14159
}

Query:                                  Result:

$.name                                  == "bradford"
$.someArray                             == "[\"array\", \"values\"]"
$.someArray[0]                          == "some"
$.someArray[1]                          == "values"
$.someArray[2]                          == error
$.obj.innerKey.innerKey2                == "innerValue"
$.obj.innerKey.innerKey3[0].kindOfStuff == "neatStuff"
$.someBool                              == true
$.PI                                    == 3.14159

Example with a JSON array as root value:

JSON:

[
  "some",
  "values",
  {
    "objKey": "objValue",
    "objKey2": [{ "catstack": "lampcat" }]
  }
]

Query:                   Result:

$[0]                     == "some"
$[1]                     == "values"
$[2]                     == "{ \"objKey\": \"objValue\", \"objKey2\": [{ \"catstack\": \"lampcat\" }] }"
$[2].objKey              == "objValue"
$[2].objKey2[0]          == "{ \"catstack\": \"lampcat\" }"
$[2].objKey2[0].catstack == "lampcat"

Run tests

go test ./...

Author

👤 Bradford Lamson-Scribner

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator

Documentation

Overview

TODO: package docs

Directories

Path Synopsis
pkg
ast
Package ast TODO: package docs
Package ast TODO: package docs
dora
Package dora TODO: package docs
Package dora TODO: package docs
lexer
Package lexer TODO
Package lexer TODO
parser
Package parser TODO
Package parser TODO
token
Package token TODO
Package token TODO

Jump to

Keyboard shortcuts

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