Giter Club home page Giter Club logo

tags's Introduction

tags

Go Reference GitHub go.mod Go version Go Test Go Report Card GitHub GitHub last commit Sourcegraph CodeFactor

created by Austin Poor

A micro-helper-library for working with Go struct tags.

Installation

Install with go get:

go get github.com/a-poor/tags

Example

Here's a quick example of working with the tags library.

// Define a struct that we'll be getting the tags from
user := struct {
    ID      int    `app:"user_id"`
    Name    string `app:",omitempty"`
    Email   string `app:"user_email,omitempty"`
    NotMe   bool
    ImEmpty bool `app:""`
}{}

// Parse the struct's tags
fields := tags.ParseStructTags("app", user)

// Print out the results as JSON
data, _ := json.MarshalIndent(fields, "", "  ")
fmt.Println(string(data))
// Output: {
//   "Email": [
//     "user_email",
//     "omitempty"
//   ],
//   "ID": [
//     "user_id"
//   ],
//   "ImEmpty": [
//     ""
//   ],
//   "Name": [
//     "",
//     "omitempty"
//   ]
// }

Usage

The tags library is very small. At least for now.

There's only one struct, TagParser, which has one field, TagName, and one method, Parse.

Say, for example, we have a struct that looks like this:

type User struct {
    ID       int     `myTag:"user_id"`
    Name     string  `myTag:"name" otherTag"abc123"`
    Balance  float32 `myTag:"balance,omitempty"`
    IsActive bool    `myTag:",hello"`
}

If we wanted to get the struct tag values for myTag, we could create a new TagParser like this:

tp := tags.TagParser{TagName: "myTag"}

and then parse the struct's tags like this:

u := User{} // Create a blank user
ut := tp.Parse(u)

ut is of the type map[string][]string, where each of the map's keys are fields of the struct (with tags), and the map's values are arrays of tag values corresponding to the chosen tag, split on commas.

In our example, we would have the following result (formatted as JSON):

{
  "Balance": [
    "balance",
    "omitempty"
  ],
  "ID": [
    "user_id"
  ],
  "IsActive": [
    "",
    "hello"
  ],
  "Name": [
    "name"
  ]
}

To Do

  • Should untagged fields appear in the returned result?
  • Add more error checks
    • ie Catch panics caused by tags and return them rather than letting the panic propagate
    • Check that the passed value is a struct (not a basic type)
  • Be able to pass a pointer to a struct (without panicing)
  • Fill a struct with struct tag values?
    • ie struct would have fields name, omitempty, etc. and would be filled by position or value (like flags).

License

MIT

Contributing

Go ahead and create an issue or submit a pull request! I'd love to hear from you.

tags's People

Contributors

a-poor avatar

Watchers

 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.