Code Monkey home page Code Monkey logo

flatty's Introduction

This library maps Go variables to string maps.

  • Nested struct keys are joined with .
  • Slice and array value keys are 0-indexed (see example)
  • It will print values of pointers
  • If given a primitive of type p with value v, Flatten returns {"p": "v"}

Background

I was writing an API client for a service which expected query strings and returned XML. I wanted to define each type of request's input and output as Go structs and have google/go-querystring and encoding/xml handle (un)marshaling.

The encoding/xml package worked for my purposes, but the google/go-querystring did not format nested keys as the service expected. I could have sent a PR to google/go-querystring, but this was not feasible for reasons unrelated to that package -- nonetheless, it's a good package with a fair number of options for output format.

Flat mapping Go structs is a fairly straightforward exercise with Go reflection and writing fewer than 100 lines-of-code was the path of least resistance. This library is slightly more general than google/go-querystring because it will output a string map rather than query params and will accept primitives in addition to structs.

Example

type AStruct struct {
    A string
    B []string
    C CStruct
}

type CStruct struct {
    D int
    E bool
}

s := &AStruct{
    A: "hello",
    B: []string{"x", "y", "z"},
    C: CStruct{
        D: 7,
        E: true,
    },
}

o := flat.Flatten(s)

// value of `o`
//   map[string]string{
//     "A": "hello",
//     "B.0": "x",
//     "B.1": "y",
//     "B.2": "z",
//     "C.D": "7",
//     "C.E": "true",
//   }

flatty's People

Contributors

rchowell avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.