Popularity
0.4
Stable
Activity
6.1
-
4
1
0

Programming language: Go
License: BSD 3-clause "New" or "Revised" License
Tags: Go Tools    

textra alternatives and similar packages

Based on the "Go Tools" category.
Alternatively, view textra alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of textra or a related project?

Add another 'Go Tools' Package

README

Textra

Textra is a simple and fast tag extractor package that helps to work with structs tags. It also has json tags for all structs, in case of JSON output.

Initially I built it for another private project, but decided to try to open-source it, since it could be useful in some use-cases. Because of that it has some features that feels redundant, like having field type as a part of returned data

Documentation

Go Reference

Installation

go get github.com/Ravcii/textra

Examples

Basic usage:

type Tester struct {
    NoTags   bool
    WithTag  string `json:"with_tag,omitempty"`
    WithTags string `json:"with_tags"          sql:"with_tag"`
    SqlOnly  string `sql:"sql_only"`
}

func main() {
    fields := textra.Extract((*Tester)(nil))
    for _, field := range fields {
        fmt.Println(field)
    }
}

NoTags(bool):[]
WithTag(string):[json:"with_tag,omitempty"]
WithTags(string):[json:"with_tags" sql:"with_tag"]
SqlOnly(string):[sql:"sql_only"]

You can look at return types at go.doc, basically it returns a slice of fields with its types (as strings) and a slice of Tags for each field.

Now let's apply some functions:

fields := textra.Extract((*Tester)(nil)).RemoveEmpty()
WithTag(string):[json:"with_tag,omitempty"]
WithTags(string):[json:"with_tags" sql:"with_tag"]
SqlOnly(string):[sql:"sql_only"]

What if we care only about SQL tags?

fields := textra.Extract((*Tester)(nil)).RemoveEmpty().Only("sql")
{WithTags string sql:"with_tag"}
{SqlOnly string sql:"sql_only"}

Only() is a bit special as it returns a Field of a different type, with Tag rather than Tags

Although it may be redundant, it also parses types a their string representation (for easier comparsion or output, if you need it)

type Types struct {
    intType         int
    byteType        byte
    runeType        rune
    stringType      string
    booleanType     bool
    sliceStringType []string
    mapType         map[string]string
    chanType        chan int
    funcType        func() error
    importType      time.Time
    pointerType     *string
}

func main() {
    fields := textra.Extract((*Types)(nil))
    for _, field := range fields {
        fmt.Println(field.Name, field.Type)
    }
}
intType int
byteType uint8
runeType int32
stringType string
booleanType bool
sliceStringType slice
mapType map
chanType chan
funcType func
importType time.Time
pointerType *string

TODO

  • [x] Better README.md (i hope)
  • [x] Add blacklisting (RemoveFields)
  • [ ] Examples for go.dev
  • [x] Some sugar for common tags
    • [x] ByName to get tag for each field
    • [x] Omitempty for "*,omitempty"
    • [x] Ignored for "-"
  • [x] Better string representation
  • [ ] ...?