kikinteractive/go-optikon

Name: go-optikon

Owner: Kik Interactive

Description: Manipulating deep Go structures using simple relative path selectors. Useful for REST-ful services, where you want to select/update/delete internal structures having a relative path to them.

Created: 2015-06-04 11:12:20.0

Updated: 2017-12-20 09:00:00.0

Pushed: 2016-06-09 14:28:32.0

Homepage:

Size: 50

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

go-optikon GoDoc Build Status Coverage Status sourcegraph views

Manipulating deep Go structures using simple relative path selectors. Useful for REST-ful services, where you want to select/update/delete internal structures having a relative path to them.

Optikon Image

Installation
t the package
 get github.com/rounds/go-optikon

wnload package dependencies
 get -t ./...

ild
 build

n tests
 test -v -cover 
Dependencies
Usage

Add the following line in your Go file:

rt "github.com/rounds/go-optikon"
Functions
 Select(dataIn interface{}, path []string) (interface{}, error)
 UpdateJSON(dataIn interface{}, path []string, dataJSON json.RawMessage, opType OpType) error
Examples
Our data model
 OrderedProduct struct {
SKU         string  `json:"sku"`
Quantity    int     `json:"quantity"`
SubTotal    float64 `json:"subtotal"`

 Order struct {
ID          string                  `json:"id"`
Date        string                  `json:"date"`
Products    []OrderedProduct        `json:"products"`
Total       float64                 `json:"total"`
ExtraProps  map[string]interface{}  `json:"extraprops"`

 Address struct {
Street  string  `json:"street"`
City    string  `json:"city"`
Country string  `json:"country"`

 Customer struct {
ID          string  `json:"id"`
Name        string  `json:"name"`
AvatarURL   string  `json:"avatar"`
Address     Address `json:"address"`
Orders      []Order `json:"orders"`


ample Customer instance.
omer := Customer {
ID:             "a12345",
Name:           "John Doe", 
AvatarURL:      "http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50",
Address: Address {
    Street:     "Pine St. 18",
    City:       "Sherwood",
    Country:    "Narnia",
},
Orders: []Order{
    Order {
        ID:     "o12345",
        Date:   "2015-06-01T23:20:22Z",
        Products: []OrderedProduct {
            OrderedProduct{
                SKU:        "1234-1234-1234",
                Quantity:   1,
                SubTotal:   12.40,
            },
            OrderedProduct{
                SKU:        "3456-3456-3456",
                Quantity:   3,
                SubTotal:   54.60,
            },
        },
        Total: 67.0,
        ExtraProps: map[string]interface{}{
            "shipping company": "EMS",
            "packaging cost":   12.20,
            "permits":  map[string]interface{}{
                "export permit": "XO-1221",
                "shipping permit": 4324234432,
            },
        },
    },
},

Select
etting the Address object:
EST: GET /customers/a12345/address
, err = optikon.Select(customer, []string{"address"})

etting the first customer order
EST: GET /customers/a12345/orders/0
r, err := optikon.Select(customer, []string{"orders", "0"})

etting the first product from the first customer order
EST: GET /customers/a12345/orders/0/products/0
, err := optikon.Select(customer, []string{"orders", "0", "products", "0"})

etting export permit code for the first order
EST: GET /customers/a12345/orders/0/extraprops/permits/export%20permit
, err := optikon.Select(customer, []string{"orders", "0", "extraprops", "permits", "export permit"})
Update
pdate address
EST: PATCH /customers/a12345/address
JSON := `{
"street": "another street",
"city": "another city",
"country": "another country"

= optikon.UpdateJSON(customer, []string{"address"}, []byte(addrJSON), optikon.UpdateOp)

pdate the first ordered product in the first customer order
EST: PATCH /customers/a12345/orders/0/products/0
JSON := `{
"sku": "4321-4321-4321",
"quantity": 1,
"subtotal": 12.40

= optikon.UpdateJSON(customer, []string{"orders", "0", "products", "0"}, []byte(prodJSON), optikon.UpdateOp)

pdate the export permit code in the first order
EST: PATCH /customers/a12345/orders/0/extraprops/permits/export%20permit
= optikon.UpdateJSON(customer, []string{"orders", "0", "extraprops", "permits", "export permit"}, []byte("XO-2222"), optikon.UpdateOp)
Create
reate a new order
EST: POST /customers/a12345/orders
rJSON := `{
"id": "o12347",
"date": "2015-06-01T10:10:10Z",
"products": [
    {
        "sku": "2345-2345-2345",
        "quantity": 2,
        "subtotal": 100.0
    }
],
"total": 100.0,
"extraprops": {
    "shipping company": "USPost",
    "packaging cost":   0
}

= optikon.UpdateJSON(customer, []string{"orders"}, []byte(orderJSON), optikon.CreateOp)

dd a property to ExtraProps of the first order
EST: POST /customers/a12345/orders/0/extra%20props
JSON := `{
"new property": "value"

= optikon.UpdateJSON(customer, []string{"orders", "0", "extraprops"}, []byte(orderJSON), optikon.CreateOp)
Delete
elete the first product from the first customer's order
EST: DELETE /customers/a12345/orders/0/products/0
= optikon.UpdateJSON(customer, []string{"orders", "0", "products", "0"}, nil, optikon.DeleteOp)

elete the shipping permit property from permits in ExtraProps of the first order
EST: DELETE /customers/a12345/orders/0/extra%20props/permits/shipping%20permit
= optikon.UpdateJSON(customer, []string{"orders", "0", "extra props", "permits", "shipping permit"}, nil, optikon.DeleteOp)
Contribute

Please check the issues page which might have some TODOs. Feel free to file new bugs and ask for improvements. We welcome pull requests!


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.