textra

package module
v0.0.0-...-81a5ce1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Textra

Textra is a simple and fast ***t***ags ***extra***ctor package that helps to work with structs tags.

Textra parses struct tags and returns them as a slice. It also does provide extra functionality like filtering.

Gathering json tags to feed it some other service is probably the most common usecase of this.

Initially I built it for another private project, but decided to try open-source it, since it could be useful in some use-cases

Badges

Go Reference

Installation

go get github.com/Ravcii/textra

Examples

Basic usage:

package main

import (
	"fmt"

	"github.com/Ravcii/textra"
)

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() {
	tags := textra.Extract((*Tester)(nil))
	fmt.Println("Basic: ", tags)
}

Basic: 		 map[SqlOnly:[sql:sql_only] WithTag:[json:with_tag,omitempty] WithTags:[json:with_tags sql:with_tag]]

Improvements

  • Better README.md
  • Examples for go.dev
  • Some sugar for common tags
    • ByName to get tag for each field
    • Omitempty for "*,omitempty"
    • Ignored for "-"
  • JSON tags in case of exporting
  • Better string representation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StructTags

type StructTags map[string]Tags

StructTags is a map that stores a slice of tags related to each field in a struct.

func Extract

func Extract(str any) StructTags

Extract accept a struct (or a pointer to a struct) and returns a map of fields and their tags.

func (StructTags) Filter

func (m StructTags) Filter(tag string) StructTags

Filter returns a map of fields and their tags, if a field has given tag.

func (StructTags) FilterFunc

func (m StructTags) FilterFunc(fn func(string, Tags) bool) StructTags

FilterFunc returns a map of fields and associated tags for given tag keys. fn is called for each field to decide whether that field should be included or not.

func (StructTags) FilterMany

func (m StructTags) FilterMany(tags ...string) StructTags

FilterMany returns a map of fields and associated tags for given tag keys. If no tags are passed, nil is returned.

type Tag

type Tag struct {
	Tag      string   `json:"tag"`
	Value    string   `json:"value"`
	Optional []string `json:"optional,omitempty"`
}

Tag represents a single struct tag, like

json:"value".

func (Tag) Ignored

func (t Tag) Ignored() bool

Ignored is a shortcut for t.Value == "-".

func (Tag) OmitEmpty

func (t Tag) OmitEmpty() bool

OmitEmpty returns true if t.Optional contains "omitempty".

func (Tag) String

func (t Tag) String() string

type Tags

type Tags []Tag

Tags is a slice of tags.

func (Tags) ByName

func (t Tags) ByName(name string) (Tag, bool)

ByName returns a tag by its name, if it exists.

Jump to

Keyboard shortcuts

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