stragts

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2022 License: BSD-3-Clause Imports: 8 Imported by: 0

README ¶

stragts

Go module providing a structured value parser for reflect.StructTag.

📦 Installation

$ go get -u github.com/0x5a17ed/stragts@latest 

🤔 Usage

package main

import (
	"fmt"
	"reflect"

	"github.com/0x5a17ed/stragts"
)

type TagStruct struct {
	Index    *string
	Priority *int
}

func HandleStruct(data any) {
	v := reflect.ValueOf(data)
	t := v.Type()
	for i := 0; i < t.NumField(); i++ {
		tag, found := stragts.Lookup(t.Field(i).Tag, "norm")
		if !found {
			continue
		}

		var tagStruct TagStruct
		if err := tag.Fill(&tagStruct); err != nil {
			fmt.Printf("error: %s\n", err)
			continue
		}

		fmt.Println("index:", *tagStruct.Index, "priority:", *tagStruct.Priority)
	}
}

func main() {
	type TaggedStruct struct {
		Name   string `norm:"index=idx_member,priority=2"`
		Number string `norm:"index=idx_member,priority=1"`
	}
	HandleStruct(TaggedStruct{})
}

🥇 Acknowledgments

The design and the implementation are roughly based on the idea and syntax of the lovely github.com/muir/reflectutils module with an implementation based on the amazing text/template/parse. Both projects have been very inspirational.

The only reason for me to write my own module was because reflectutils was missing a few features, and I was always looking for an opportunity to write a lexer.

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var (
	ErrPositionalAfterKeyword = errors.New("positional argument after keywords")
)

Functions ¶

func Parse ¶

func Parse(inp string) (*tree, error)

Types ¶

type Tag ¶

type Tag struct {
	Name  string
	Value string
}

func Lookup ¶

func Lookup(tags reflect.StructTag, key string) (t *Tag, ok bool)

func (Tag) Fill ¶

func (tag Tag) Fill(model any) error

Directories ¶

Path Synopsis

Jump to

Keyboard shortcuts

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