8000 GitHub - dergus/tagx: Extract tag values from any Go struct fields · GitHub
[go: up one dir, main page]

Skip to content
dergusPublic

About

Extract tag values from any Go struct fields

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TagX

Extract tag values from any Go struct fields

Install

go get github.com/dergus/tagx

Features

  • Extract tag values by a tag key as a hierarchical data structure
  • Extract tag values by a tag key as a flat array of strings concatenated by a given delimiter

Usage

Extract

type S struct {
    FieldA       int `json:"field_a"`
    NestedStruct struct {
    NestedFieldA        string `json:"nested_field_a"`
        AnotherNestedStruct struct {
        AnotherNestedField int `json:"another_nested_field"`
        } `json:"another_nested_struct"`
    } `json:"nested_struct"`
}

tags := tagx.Extract(S{}, "json")

// tags
[]tagx.Tag{
	{
		Value:"field_a",
	},
	{
		Value:"nested_struct",
		Children: []tagx.Tag{
			{
				Value: "nested_field_a",
			},
			{
				Value: "another_nested_struct",
				Children: []tagx.Tag{
					{
						Value:"another_nested_field",
					}
                }
            }
        }
	}
}

ExtractFlat

type S struct {
    FieldA       int `json:"field_a"`
    NestedStruct struct {
    NestedFieldA        string `json:"nested_field_a"`
        AnotherNestedStruct struct {
        AnotherNestedField int `json:"another_nested_field"`
        } `json:"another_nested_struct"`
    } `json:"nested_struct"`
}

tags := tagx.ExtractFlat(S{}, "json", ".")

// tags
[]string{
    "field_a",
    "nested_struct.nested_field_a",
    "nested_struct.another_nested_struct.another_nested_field",
}

Caveats

  • Only tag value is returned, options(e.g. omitempty for the json tag) are omitted
  • Private Fields (with tag value -) are ignored and not returned
  • Empty tag values or just whitespace values are ignored and not returned
  • Fields of recursive structs aren't explored

License

Copyright © 2023 Ziyadin Shemsedinov.

This project is MIT licensed.

About

Extract tag values from any Go struct fields

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

0