gabstv / tagq

Tagq is a package that traverses structs, maps and slices using reflection.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tagq

Coverage Badge

package main

import (
    "fmt"
    "time"

    "github.com/gabstv/tagq"
)

type Item struct {
    Name string `json:"jname" xml:"xname"`
    Score int
    ScoreHist []int
    Nested struct{
        Moment time.Time `json:"m_oment"`
        Title string
    }
}

func main() {
    item := &Item{}
    item.Name = "Example"
    item.Score = 100
    item.ScoreHist = []int{80, 5, 15}
    item.Nested.Moment = time.Now()
    item.Nested.Title = "Hello"

    fmt.Println(tagq.Q(item, "jname").Str()) // prints: Example
    fmt.Println(tagq.Q(item, "xname").Str()) // prints: Example
    fmt.Println(tagq.Q(item, "Name").Str()) // prints: Example
    fmt.Println(tagq.Q(item, "Name", "something").Str()) // prints: ""
    fmt.Println(tagq.Q(item, "Score").Int()) // prints: 100
    fmt.Println(tagq.Q(item, "ScoreHist", "last").Int()) // prints: 15
    fmt.Println(tagq.Q(item, "ScoreHist", "0").Int()) // prints: 80
    fmt.Println(tagq.Q(item, "Nested", "m_oment").Time())
    fmt.Println(tagq.Q(item, "Nested", "Title").Str()) // prints: Hello
}

About

Tagq is a package that traverses structs, maps and slices using reflection.


Languages

Language:Go 100.0%