rvflash / flat

Flat provides methods to handle JSON, XML or YAML data as a map[string]interface

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flat

GoDoc Build Status Code Coverage Go Report Card

The package flat provides methods to handle JSON, XML or YAML data as a map[string]interface{}, useful to manipulate unknown structures or to flatten them into a single dimension.

Installation

To install it, you need to install Go and set your Go workspace first. Then, download and install it:

$ go get -u github.com/rvflash/flat

Import it in your code:

import "github.com/rvflash/flat"

Prerequisite

flat uses the Go modules that required Go 1.11 or later.

XML Samples (see the example tests)

Errors are ignored just for the demo.

Marshal

var (
    d = map[string]interface{}{
        "languages": map[string]interface{}{
            "en": "English",
            "fr": "French",
        },
    }
    res   = bytes.Buffer{}
    attrs = []xml.Attr{
        {Name: xml.Name{Local: "xmlns:xsi"}, Value: "http://www.w3.org/2001/XMLSchema-instance"},
    }
    _ = flat.New(
        d,
        flat.XMLName("custom"),
        flat.XMLNS("http://schemas.xmlsoap.org/soap/envelope/"),
        flat.XMLAttributes(attrs),
    ).XMLEncode(&res)
)
fmt.Println(res.String())
// Output:
// <custom xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><languages><en>English</en><fr>French</fr></languages></custom>

Unmarshal

var (
    d   = flat.D{}
    _ = xml.Unmarshal([]byte(`<custom><languages><en>English</en><fr>French</fr></languages></custom>`), &d)
)
fmt.Printf("%#v", d.Flatten())
// Output:
// map[string]interface {}{"languages_en":"English", "languages_fr":"French"}

About

Flat provides methods to handle JSON, XML or YAML data as a map[string]interface

License:MIT License


Languages

Language:Go 100.0%