Compare commits
3 Commits
v0.2.0
...
da009aae4f
| Author | SHA1 | Date | |
|---|---|---|---|
| da009aae4f | |||
| 754642e98f | |||
| 16d52188f6 |
+17
-1
@@ -45,6 +45,14 @@ func writeFieldsTo(w io.Writer, fields []Field, nl string) (int64, error) {
|
|||||||
return int64(written), nil
|
return int64(written), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String generates a string output for "%s"
|
||||||
|
func (field Field) String() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
_, _ = writeFieldsTo(&buf, []Field{field}, WriteNewLine)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
func writeSectionToBuffer(w *bytes.Buffer, sec *Section, nl string) int {
|
func writeSectionToBuffer(w *bytes.Buffer, sec *Section, nl string) int {
|
||||||
var written, n int
|
var written, n int
|
||||||
|
|
||||||
@@ -74,6 +82,14 @@ func writeSectionToBuffer(w *bytes.Buffer, sec *Section, nl string) int {
|
|||||||
return written + int(n64)
|
return written + int(n64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String generates a string output for "%s"
|
||||||
|
func (sec *Section) String() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
_ = writeSectionToBuffer(&buf, sec, WriteNewLine)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
// WriteTo writes a INI representation of the document
|
// WriteTo writes a INI representation of the document
|
||||||
// onto the provided writer.
|
// onto the provided writer.
|
||||||
func (doc *Document) WriteTo(w io.Writer) (int64, error) {
|
func (doc *Document) WriteTo(w io.Writer) (int64, error) {
|
||||||
@@ -81,7 +97,7 @@ func (doc *Document) WriteTo(w io.Writer) (int64, error) {
|
|||||||
return buf.WriteTo(w)
|
return buf.WriteTo(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GoString generates a string output for "%s"
|
// String generates a string output for "%s"
|
||||||
func (doc *Document) String() string {
|
func (doc *Document) String() string {
|
||||||
buf := doc.AsBuffer(WriteNewLine)
|
buf := doc.AsBuffer(WriteNewLine)
|
||||||
return buf.String()
|
return buf.String()
|
||||||
|
|||||||
+74
@@ -0,0 +1,74 @@
|
|||||||
|
package ini
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"asciigoat.org/core"
|
||||||
|
"asciigoat.org/core/reflection"
|
||||||
|
"asciigoat.org/ini/parser"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Decoder ...
|
||||||
|
type Decoder[T any] struct {
|
||||||
|
io.Closer
|
||||||
|
|
||||||
|
out *reflection.Reflection[T]
|
||||||
|
p *parser.Parser
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode ...
|
||||||
|
func (dec *Decoder[T]) Decode(v *T) error {
|
||||||
|
defer dec.Close()
|
||||||
|
|
||||||
|
r, err := reflection.New(v)
|
||||||
|
switch e := err.(type) {
|
||||||
|
case *reflection.InvalidUnmarshalError:
|
||||||
|
// customize error
|
||||||
|
e.Prefix = "ini"
|
||||||
|
e.Method = "Decode"
|
||||||
|
return e
|
||||||
|
case nil:
|
||||||
|
// good reflection. Go!
|
||||||
|
dec.out = r
|
||||||
|
return dec.p.Run()
|
||||||
|
default:
|
||||||
|
// other errors
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDecoder creates a Decoder using the provided [io.Reader]
|
||||||
|
// as source
|
||||||
|
func NewDecoder[T any](r io.Reader) *Decoder[T] {
|
||||||
|
rc := core.NewReadCloser(r)
|
||||||
|
switch {
|
||||||
|
case rc == nil:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
dec := &Decoder[T]{
|
||||||
|
p: parser.NewParser(rc),
|
||||||
|
Closer: rc,
|
||||||
|
}
|
||||||
|
dec.init()
|
||||||
|
return dec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dec *Decoder[T]) init() {
|
||||||
|
dec.p.OnToken = dec.parserOnToken
|
||||||
|
dec.p.OnError = dec.parserOnError
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDecoderBytes creates a Decoder using the provided bytes array
|
||||||
|
// as source
|
||||||
|
func NewDecoderBytes[T any](b []byte) *Decoder[T] {
|
||||||
|
return NewDecoder[T](bytes.NewReader(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDecoderString creates a Decoder over a provided string of data
|
||||||
|
func NewDecoderString[T any](s string) *Decoder[T] {
|
||||||
|
return NewDecoder[T](strings.NewReader(s))
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package ini
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"asciigoat.org/core/lexer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (*Decoder[T]) parserOnError(pos lexer.Position, content string, err error) error {
|
||||||
|
log.Printf("%s: %s %s: %q: %v", "ini", pos, "error:", content, err)
|
||||||
|
|
||||||
|
return &lexer.Error{
|
||||||
|
Line: pos.Line,
|
||||||
|
Column: pos.Column,
|
||||||
|
Content: content,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package ini
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"asciigoat.org/core/lexer"
|
||||||
|
"asciigoat.org/ini/parser"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (*Decoder[T]) parserOnToken(pos lexer.Position, typ parser.TokenType, value string) error {
|
||||||
|
log.Printf("%s: %s %s %q", "ini", pos, typ, value)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@ module asciigoat.org/ini
|
|||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
|
replace asciigoat.org/core => ../core
|
||||||
|
|
||||||
require (
|
require (
|
||||||
asciigoat.org/core v0.3.7
|
asciigoat.org/core v0.3.7
|
||||||
github.com/mgechev/revive v1.3.3
|
github.com/mgechev/revive v1.3.3
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
asciigoat.org/core v0.3.7 h1:tMasdvZgsMJJMVsZVfXXB5lqq82pFiCsyEmOEmcmAfI=
|
|
||||||
asciigoat.org/core v0.3.7/go.mod h1:tXj+JUutxRbcO40ZQRuUVaZ4rnYz1kAZ0nblisV8u74=
|
|
||||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
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 h1:5JxePczlyGAtj6R1MUEFZ/UFud6FfsOejq7xLC2ZIb0=
|
||||||
|
|||||||
Reference in New Issue
Block a user