tags

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MIT Imports: 4 Imported by: 3

README

Tags

GoDoc Go Report Card License

A simple tags parser for golang's struct

Install

go get github.com/go-mods/tags

Example

package main

import (
    "fmt"
    "github.com/go-mods/tags"
    "reflect"
)

func main() {
    type Employee struct {
        Id   int    `json:"id" xml:"id" excel:"id"`
        Name string `json:"name,string" xml:"name" excel:"name"`
        Age int `json:"age,omitempty" xml:"age" excel:"column:age"`
    }

    // Loop throw all fields
    for i := 0; i < reflect.TypeOf(Employee{}).NumField(); i++ {
        // get the field
        field := reflect.TypeOf(Employee{}).Field(i)
        // get the tag field
        tag := field.Tag

        // parse it
        tgs, err := tags.Parse(string(tag))
        if err != nil {
            panic(err)
        }

        // iterate over all tags
        fmt.Println(fmt.Sprintf("// Tags for field: %s", field.Name))
        for _, t := range tgs {
            out, _ := json.Marshal(t)
            fmt.Println(string(out))
        }
        fmt.Println("")
    }
}

// OUTPUT

// Tags for field: Id
{"Tag":"json:\"id\"","Key":"json","Value":"id","Name":"id","Options":null}
{"Tag":"xml:\"id\"","Key":"xml","Value":"id","Name":"id","Options":null}
{"Tag":"excel:\"id\"","Key":"excel","Value":"id","Name":"id","Options":null}

// Tags for field: Name
{"Tag":"json:\"name,string\"","Key":"json","Value":"name,string","Name":"name","Options":[{"Key":"string","Value":""}]}
{"Tag":"xml:\"name\"","Key":"xml","Value":"name","Name":"name","Options":null}
{"Tag":"excel:\"name\"","Key":"excel","Value":"name","Name":"name","Options":null}

// Tags for field: Age
{"Tag":"json:\"age,omitempty\"","Key":"json","Value":"age,omitempty","Name":"age","Options":[{"Key":"omitempty","Value":""}]}
{"Tag":"xml:\"age\"","Key":"xml","Value":"age","Name":"age","Options":null}
{"Tag":"excel:\"column:age\"","Key":"excel","Value":"column:age","Name":"","Options":[{"Key":"column","Value":"age"}]}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option struct {
	Key   string
	Value any
}

Option is a simple key, value pair

type Tag

type Tag struct {
	// Tag is the full string containing the Key, the Name and Options
	Tag string

	// Key is the tag key which can be obtained with func (StructTag) Get
	// in `json:"id,omitempty"`, the Key is "json"
	// in `gorm:"embedded;embeddedPrefix:author_"`, the Key is "gorm"
	// in `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`, the Key is "gorm"
	Key string

	// Value is obtained with func (StructTag) Lookup
	// in `json:"id,omitempty"`, the Value is "id,omitempty"
	// in `gorm:"embedded;embeddedPrefix:author_"`, the Value is "embedded;embeddedPrefix:author_"
	// in `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`, the Value is "constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"
	Value string

	// Name is the first part of the value obtained with func (StructTag) Lookup
	// in `json:"id,omitempty"`, the key Name "id"
	// in `gorm:"embedded;embeddedPrefix:author_"`, the Name is "embedded"
	// in `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`, the Name is "constraint"
	Name string

	// Options are the second part of the value obtained with func (StructTag) Lookup
	// Options is a list of options (Key, Value pair)
	// in `json:"id,omitempty"`, the Options is ["omitempty"]
	// in `gorm:"embedded;embeddedPrefix:author_"`, the Options is ["embeddedPrefix:author_"]
	// in `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`, the Options is ["OnUpdate:CASCADE", "OnDelete:SET NULL;"]
	Options []*Option
}

Tag represents a string literal applied to a struct field see: https://pkg.golang.ir/reflect#StructTag

func Lookup

func Lookup(field reflect.StructField, key string) *Tag

func Parse

func Parse(tags string) ([]*Tag, error)

func (*Tag) GetOption

func (tags *Tag) GetOption(option string) *Option

GetOption return the option or nil

func (*Tag) HasOption

func (tags *Tag) HasOption(option string) bool

HasOption return true if the option is found

Jump to

Keyboard shortcuts

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