structtags

package module
v0.0.0-...-a76a5db Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2017 License: MIT Imports: 1 Imported by: 2

README

structtags

Go Report Card Codecov Build Status Go docs

A go package for parsing struct tags based on the pattern used in the encoding packages of the go stdlib.

Usage

See the example in the Go Docs.

Documentation

Overview

Package structtags parses struct tags based on the pattern used in the encoding packages of the go stdlib.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tag

type Tag struct {
	Value   string
	Options TagOptions
}

Tag contains a tag value and options.

func Parse

func Parse(tag string) Tag

Parse parses a string tag into a Tag struct. A tag defined as `myTag:"value,abc,def"` will be parsed as:

Tag {
  Value: "value",
  Options: ["abc", "def"],
}
Example
package main

import (
	"fmt"
	"reflect"

	"4d63.com/structtags"
)

func main() {
	s := struct {
		A string `myTag:"a,omitempty,flatten"`
		B string `myTag:",omitempty"`
		C int
	}{}

	t := reflect.TypeOf(s)

	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)

		if tagStr, ok := f.Tag.Lookup("myTag"); ok {
			tag := structtags.Parse(tagStr)

			fmt.Printf("Tag: %#v\n", tag.Value)
			fmt.Println("  Omit Empty:", tag.Options.Contains("omitempty"))
			fmt.Println("  Flatten:", tag.Options.Contains("flatten"))
		}
	}

}
Output:

Tag: "a"
  Omit Empty: true
  Flatten: true
Tag: ""
  Omit Empty: true
  Flatten: false

type TagOptions

type TagOptions []string

TagOptions contains the options for a tag.

func (TagOptions) Contains

func (o TagOptions) Contains(option string) bool

Contains checks if the option is contained in the TagOptions.

Jump to

Keyboard shortcuts

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