Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6df387ffeb | |||
| 123770119e | |||
| 667803fea5 | |||
| a38f0c74e8 | |||
| 7e0b7cfb47 | |||
| fd4f6439b8 | |||
| f74745471d |
@@ -2,22 +2,21 @@ module asciigoat.org/core
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/fatih/structtag v1.2.0
|
||||
github.com/mgechev/revive v1.3.3
|
||||
github.com/pkg/errors v0.9.1
|
||||
)
|
||||
require github.com/mgechev/revive v1.3.3
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab // indirect
|
||||
github.com/chavacava/garif v0.1.0 // indirect
|
||||
github.com/fatih/color v1.15.0 // indirect
|
||||
github.com/fatih/structtag v1.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.9 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.4 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
golang.org/x/tools v0.12.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab h1:5JxePczlyGAtj6R1MUEFZ/UFud6FfsOejq7xLC2ZIb0=
|
||||
github.com/chavacava/garif v0.0.0-20230608123814-4bd63c2919ab/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
|
||||
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
|
||||
github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -12,10 +12,11 @@ github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0=
|
||||
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
|
||||
github.com/mgechev/revive v1.3.3 h1:GUWzV3g185agbHN4ZdaQvR6zrLVYTUSA2ktvIinivK0=
|
||||
@@ -28,6 +29,9 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
|
||||
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
@@ -37,6 +41,7 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package parser implements some common logic of asciigoat based parsers
|
||||
package parser
|
||||
@@ -0,0 +1,72 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"asciigoat.org/core/lexer"
|
||||
)
|
||||
|
||||
// TextParser ...
|
||||
type TextParser struct {
|
||||
src *lexer.Reader
|
||||
pos lexer.Position
|
||||
}
|
||||
|
||||
// Position ...
|
||||
func (p *TextParser) Position() lexer.Position {
|
||||
return p.pos
|
||||
}
|
||||
|
||||
// StepLine ...
|
||||
func (p *TextParser) StepLine() {
|
||||
p.src.Discard()
|
||||
p.pos.StepLine()
|
||||
}
|
||||
|
||||
// Step ...
|
||||
func (p *TextParser) Step() {
|
||||
s := p.src.Emit()
|
||||
p.pos.StepN(len(s))
|
||||
}
|
||||
|
||||
// Emit ...
|
||||
func (p *TextParser) Emit() Token {
|
||||
t := Token{
|
||||
Value: p.src.Emit(),
|
||||
Position: p.pos,
|
||||
}
|
||||
|
||||
p.pos.StepN(t.Len())
|
||||
return t
|
||||
}
|
||||
|
||||
// Peek ...
|
||||
func (p *TextParser) Peek() string {
|
||||
return p.src.String()
|
||||
}
|
||||
|
||||
// NewTextParser ...
|
||||
func NewTextParser(r io.Reader) *TextParser {
|
||||
if r == nil {
|
||||
r = strings.NewReader("")
|
||||
}
|
||||
|
||||
p := &TextParser{
|
||||
src: lexer.NewReader(r),
|
||||
}
|
||||
p.pos.Reset()
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// NewTextParserBytes ...
|
||||
func NewTextParserBytes(b []byte) *TextParser {
|
||||
return NewTextParser(bytes.NewReader(b))
|
||||
}
|
||||
|
||||
// NewTextParserString ...
|
||||
func NewTextParserString(s string) *TextParser {
|
||||
return NewTextParser(strings.NewReader(s))
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"asciigoat.org/core/lexer"
|
||||
)
|
||||
|
||||
// Token ...
|
||||
type Token struct {
|
||||
lexer.Position
|
||||
|
||||
Type uint
|
||||
Value string
|
||||
}
|
||||
|
||||
// Len ...
|
||||
func (t Token) Len() int {
|
||||
return len(t.Value)
|
||||
}
|
||||
|
||||
// String generates the '%v' value
|
||||
func (t Token) String() string {
|
||||
return t.Value
|
||||
}
|
||||
|
||||
// GoString generates the '%#v' value
|
||||
func (t Token) GoString() string {
|
||||
return fmt.Sprintf("Token{Position{%v,%v}, %v, %q}",
|
||||
t.Line, t.Column, t.Type, t.Value)
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func structFieldName(sf reflect.StructField) string {
|
||||
if sf.Anonymous {
|
||||
idx := sf.Index[len(sf.Index)-1]
|
||||
|
||||
return fmt.Sprintf("%s-%v", "anonymous", idx)
|
||||
}
|
||||
|
||||
return sf.Name
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
// Package reflection helps Marshalling/Unmarshalling data to structs
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Reflection provides Marshalling/Unmarshalling oriented view
|
||||
// of a value
|
||||
type Reflection struct {
|
||||
v reflect.Value
|
||||
|
||||
types map[reflect.Type]TypeInfo
|
||||
}
|
||||
|
||||
// Value returns the object it reflects
|
||||
func (r *Reflection) Value() any {
|
||||
return r.v.Interface()
|
||||
}
|
||||
|
||||
// New creates a Reflection of the given pointer
|
||||
func New(v any) (*Reflection, error) {
|
||||
rv := reflect.ValueOf(v)
|
||||
|
||||
if rv.Kind() != reflect.Pointer || rv.IsNil() {
|
||||
err := &InvalidUnmarshalError{Type: rv.Type()}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := &Reflection{
|
||||
v: rv,
|
||||
types: make(map[reflect.Type]TypeInfo),
|
||||
}
|
||||
|
||||
if err := r.scan(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func (r *Reflection) scanType(rt reflect.Type) error {
|
||||
switch rt.Kind() {
|
||||
case reflect.Pointer, reflect.Slice:
|
||||
rt = rt.Elem()
|
||||
}
|
||||
|
||||
if _, ok := r.types[rt]; ok {
|
||||
// cached
|
||||
return nil
|
||||
}
|
||||
|
||||
ti, err := NewTypeInfo(rt)
|
||||
if err != nil {
|
||||
return &UnmarshalTypeError{Type: rt, Err: err}
|
||||
}
|
||||
|
||||
r.types[rt] = ti
|
||||
|
||||
for i := range ti.fields {
|
||||
err := r.scanType(ti.fields[i].sf.Type)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Reflection) scan() error {
|
||||
return r.scanType(r.v.Type())
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
package reflection
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
"github.com/fatih/structtag"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// TypeInfo ...
|
||||
type TypeInfo struct {
|
||||
rt reflect.Type
|
||||
|
||||
fields []TypeInfoField
|
||||
}
|
||||
|
||||
// Type ...
|
||||
func (ti *TypeInfo) Type() reflect.Type {
|
||||
return ti.rt
|
||||
}
|
||||
|
||||
// Kind ...
|
||||
func (ti *TypeInfo) Kind() reflect.Kind {
|
||||
return ti.rt.Kind()
|
||||
}
|
||||
|
||||
// TypeInfoField ...
|
||||
type TypeInfoField struct {
|
||||
sf reflect.StructField
|
||||
tags *structtag.Tags
|
||||
|
||||
Name string
|
||||
}
|
||||
|
||||
// Type ...
|
||||
func (tif *TypeInfoField) Type() reflect.Type {
|
||||
return tif.sf.Type
|
||||
}
|
||||
|
||||
// Kind ...
|
||||
func (tif *TypeInfoField) Kind() reflect.Kind {
|
||||
return tif.sf.Type.Kind()
|
||||
}
|
||||
|
||||
// Tag ...
|
||||
func (tif *TypeInfoField) Tag(key string) (*structtag.Tag, bool) {
|
||||
if tif.tags != nil {
|
||||
tag, _ := tif.tags.Get(key)
|
||||
if tag != nil {
|
||||
return tag, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// NewTypeInfo ...
|
||||
func NewTypeInfo(rt reflect.Type) (TypeInfo, error) {
|
||||
log.Printf("%s.%s: %s (%s)", "reflection", "NewTypeInfo", rt, rt.Kind())
|
||||
|
||||
ti := TypeInfo{
|
||||
rt: rt,
|
||||
}
|
||||
|
||||
err := ti.init()
|
||||
return ti, err
|
||||
}
|
||||
|
||||
func (ti *TypeInfo) init() error {
|
||||
switch ti.Kind() {
|
||||
case reflect.Struct:
|
||||
return ti.initStruct()
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (ti *TypeInfo) initStruct() error {
|
||||
// load fields
|
||||
n := ti.rt.NumField()
|
||||
fields := make([]TypeInfoField, 0, n)
|
||||
for i := 0; i < n; i++ {
|
||||
sf := ti.rt.Field(i)
|
||||
tags, err := structtag.Parse(string(sf.Tag))
|
||||
|
||||
if err != nil {
|
||||
// tag parse error
|
||||
err = &UnmarshalTypeError{
|
||||
Type: ti.rt,
|
||||
Err: errors.Wrap(err, structFieldName(sf)),
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if sf.IsExported() {
|
||||
log.Printf("%s.%s: [%v]: %q %s (%s)",
|
||||
"reflection", "NewTypeInfo",
|
||||
i, structFieldName(sf), sf.Type, sf.Type.Kind())
|
||||
|
||||
fields = append(fields, TypeInfoField{
|
||||
sf: sf,
|
||||
tags: tags,
|
||||
})
|
||||
}
|
||||
}
|
||||
ti.fields = fields
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package reflective assists reflection-driven unmarshalling
|
||||
package reflective
|
||||
@@ -1,4 +1,4 @@
|
||||
package reflection
|
||||
package reflective
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -0,0 +1,15 @@
|
||||
package reflective
|
||||
|
||||
import "reflect"
|
||||
|
||||
// IsZero tells if a value is zero either via
|
||||
// v.IsZero() or reflection.
|
||||
func IsZero(iv any) bool {
|
||||
if p, ok := iv.(interface {
|
||||
IsZero() bool
|
||||
}); ok {
|
||||
return p.IsZero()
|
||||
}
|
||||
|
||||
return reflect.ValueOf(iv).IsZero()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package reflective
|
||||
|
||||
import "reflect"
|
||||
|
||||
// Reflection provides Marshalling/Unmarshalling oriented view
|
||||
// of a value
|
||||
type Reflection struct {
|
||||
v reflect.Value
|
||||
}
|
||||
|
||||
// New creates a Reflection of the given pointer
|
||||
func New(v any) (*Reflection, error) {
|
||||
rv := reflect.ValueOf(v)
|
||||
|
||||
if rv.Kind() != reflect.Pointer || rv.IsNil() {
|
||||
err := &InvalidUnmarshalError{
|
||||
Type: rv.Type(),
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := &Reflection{
|
||||
v: rv,
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
Reference in New Issue
Block a user