Skip to content

Latest commit

 

History

History
executable file
·
59 lines (52 loc) · 1.12 KB

README.md

File metadata and controls

executable file
·
59 lines (52 loc) · 1.12 KB

parser-go-reflect

Reflection based parser for Go.

We can dynamically walk over reflecting Go structs using the Parser interface:

import (
    "reflect"
    "io"

    reflectparser "github.com/katydid/parser-go-reflect/reflect"
)

func main() {
    reflectParser := reflectparser.NewReflectParser()
    s := &MyStruct{MyField: "myvalue", OtherField: "othervalue"}
    v := reflect.ValueOf(s)
    reflectParser.Init(v)
    myvalue, err := GetMyField("MyField")
    if err != nil {
        panic(err)
    }
    println(myvalue)
}

We can then use the parser to decode only MyField and skip over other fields and return "myvalue":

func GetMyField(p parser.Interface) (string, error) {
	for {
		if err := p.Next(); err != nil {
			if err == io.EOF {
				break
			} else {
				return "", err
			}
		}
		fieldName, err := p.String()
		if err != nil {
			return "", err
		}
		if fieldName != "MyField" {
			continue
		}
		p.Down()
		if err := p.Next(); err != nil {
			if err == io.EOF {
				break
			} else {
				return "", err
			}
		}
		return p.String()
	}
	return "", nil
}