structtag

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2025 License: BSD-3-Clause Imports: 4 Imported by: 1

README

structtag PkgGoDev

structtag is a library for parsing struct tags in Go during static analysis. It is designed to provide a simple API for parsing struct field tags in Go code. This fork focuses exclusively on reading struct tags and is not intended for modifying or rewriting them. It also drops support for accessing the options of a struct tag individually.

This project is a fork of fatih/structtag, originally created by Fatih Arslan. The primary changes in this fork are:

  • struct tag values are treated as blobs, options are not treated individually,
  • the API surface is minimized, just so that the functionality used by 4meepo/tagalign is provided.

Install

To install the package:

go get github.com/alfatraining/structtag

Example Usage

The following example demonstrates how to parse and output struct tags using structtag:

package main

import (
	"fmt"
	"reflect"

	"github.com/alfatraining/structtag"
)

func main() {
	type Example struct {
		Field string `json:"foo,omitempty" xml:"bar"`
	}

	// Get the struct tag from the field
	tag := reflect.TypeOf(Example{}).Field(0).Tag

	// Parse the tag using structtag
	tags, err := structtag.Parse(string(tag))
	if err != nil {
		panic(err)
	}

	// Iterate over all tags
	for _, t := range tags.Tags() {
		fmt.Printf("Key: %s, Value: %v\n", t.Key, t.Value)
	}
	// Output:
	// Key: json, Value: foo,omitempty
	// Key: xml, Value: bar
}

API Overview

Parsing Struct Tags

Use Parse to parse a struct field's tag into a Tags object:

tags, err := structtag.Parse(`json:"foo,omitempty" xml:"bar"`)
if err != nil {
    panic(err)
}
Accessing Tags
  • Retrieve all tags:
    allTags := tags.Tags()
    
Inspecting Tags
  • Key: The key of the tag (e.g., json or xml).
  • Value: The value of the tag (e.g., "foo,omitempty" for json:"foo,omitempty").

Acknowledgments

This project is based on fatih/structtag, created by Fatih Arslan. Inspiration for the style of this README was taken from tylergannon/structtag, created by Tyler Gannon.

Documentation

Overview

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/alfatraining/structtag"
)

func main() {
	type Example struct {
		Field string `json:"foo,omitempty" xml:"bar"`
	}

	// Get the struct tag from the field
	tag := reflect.TypeOf(Example{}).Field(0).Tag

	// Parse the tag using structtag
	tags, err := structtag.Parse(string(tag))
	if err != nil {
		panic(err)
	}

	// Iterate over all tags
	for _, t := range tags.Tags() {
		fmt.Printf("Key: %s, Value: %v\n", t.Key, t.Value)
	}
}
Output:
Key: json, Value: foo,omitempty
Key: xml, Value: bar

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tag

type Tag struct {
	// Key is the tag key, such as json, xml, etc.
	// i.e: `json:"foo,omitempty". Here key is: "json"
	Key string

	// Value is the value stored for this tag key.
	// i.e.: `json:"foo,omitempty". Here the value is: "foo,omitempty".
	Value string
}

Tag defines a single struct's string literal tag

func (*Tag) String

func (t *Tag) String() string

String reassembles the tag into a valid tag field representation

type Tags

type Tags struct {
	// contains filtered or unexported fields
}

Tags represent a set of tags from a single struct field

func Parse

func Parse(tag string) (*Tags, error)

Parse parses a single struct field tag and returns the set of tags.

func (*Tags) Len

func (t *Tags) Len() int

func (*Tags) String

func (t *Tags) String() string

String reassembles the tags into a valid literal tag field representation

func (*Tags) Tags

func (t *Tags) Tags() []*Tag

Tags returns a slice of tags. The order is the original tag order.

Jump to

Keyboard shortcuts

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