Compare commits
3 Commits
226323bbc5
...
a1b118adbe
| Author | SHA1 | Date | |
|---|---|---|---|
| a1b118adbe | |||
| a95fed36dc | |||
| 70f3968fb3 |
@@ -16,6 +16,7 @@ of stricter parsers of similar form.
|
||||
|
||||
[godoc-lexer]: https://pkg.go.dev/asciigoat.org/core/lexer
|
||||
[godoc-parser-parser]: https://pkg.go.dev/asciigoat.org/ini/parser#Parser
|
||||
[godoc-basic-parser]: https://pkg.go.dev/asciigoat.org/ini/basic#Decode
|
||||
|
||||
[wikipedia-dosini]: https://en.wikipedia.org/wiki/INI_file
|
||||
|
||||
@@ -25,6 +26,14 @@ of stricter parsers of similar form.
|
||||
[`asciigoat`'s lexer][godoc-lexer] to process an `INI`-style document
|
||||
emiting tokens and errors via callbacks.
|
||||
|
||||
## Basic Parser
|
||||
|
||||
[`basic.Decode()`][godoc-basic-parser] provies a one-shot decoder
|
||||
that returns a structured document for you to post-process.
|
||||
|
||||
To allow for correct handling of repetition of section and field names downstream, it uses arrays instead of maps, and makes almost no judgment
|
||||
about what section or field names are acceptable.
|
||||
|
||||
## Other Implementations
|
||||
|
||||
* [gcfg](https://pkg.go.dev/gopkg.in/gcfg.v1)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package basic provides a basic representation of dosini-style documents
|
||||
package basic
|
||||
@@ -0,0 +1,73 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"asciigoat.org/core/lexer"
|
||||
"asciigoat.org/ini/parser"
|
||||
)
|
||||
|
||||
type token struct {
|
||||
pos lexer.Position
|
||||
typ parser.TokenType
|
||||
value string
|
||||
}
|
||||
|
||||
type decoder struct {
|
||||
p *parser.Parser
|
||||
out *Document
|
||||
|
||||
queue []token
|
||||
curSection *Section
|
||||
curField *Field
|
||||
}
|
||||
|
||||
func (dec *decoder) OnToken(pos lexer.Position, typ parser.TokenType, value string) error {
|
||||
tok := token{pos, typ, value}
|
||||
|
||||
log.Printf("%s[%v]: %s %s: %q", "decoder", len(dec.queue), pos, typ, value)
|
||||
|
||||
dec.queue = append(dec.queue, tok)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*decoder) OnError(pos lexer.Position, hint string, err error) error {
|
||||
log.Printf("%s: %s %s: %s: %v", "decoder", pos, "error", hint, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Decode ...
|
||||
func Decode(r io.Reader) (*Document, error) {
|
||||
var out Document
|
||||
|
||||
if r == nil {
|
||||
return nil, fs.ErrNotExist
|
||||
}
|
||||
|
||||
// parser
|
||||
p := parser.NewParser(r)
|
||||
// decoder
|
||||
dec := decoder{p: p, out: &out}
|
||||
// glue
|
||||
p.OnToken = dec.OnToken
|
||||
p.OnError = dec.OnError
|
||||
|
||||
// Go!
|
||||
err := p.Run()
|
||||
return &out, err
|
||||
}
|
||||
|
||||
// DecodeBytes ...
|
||||
func DecodeBytes(b []byte) (*Document, error) {
|
||||
return Decode(bytes.NewReader(b))
|
||||
}
|
||||
|
||||
// DecodeString ...
|
||||
func DecodeString(s string) (*Document, error) {
|
||||
return Decode(strings.NewReader(s))
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package basic
|
||||
|
||||
// Document ...
|
||||
type Document struct {
|
||||
Global []Field
|
||||
|
||||
Sections []Section
|
||||
}
|
||||
|
||||
// Section ...
|
||||
type Section struct {
|
||||
Name string
|
||||
Key string
|
||||
HadKey bool
|
||||
|
||||
Fields []Field
|
||||
}
|
||||
|
||||
// Field ...
|
||||
type Field struct {
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"asciigoat.org/core"
|
||||
"asciigoat.org/ini/parser"
|
||||
)
|
||||
|
||||
// Decoder ...
|
||||
type Decoder struct {
|
||||
io.Closer
|
||||
|
||||
p *parser.Parser
|
||||
}
|
||||
|
||||
// Decode ...
|
||||
func (dec *Decoder) Decode() error {
|
||||
defer dec.Close()
|
||||
|
||||
return dec.p.Run()
|
||||
}
|
||||
|
||||
// NewDecoder creates a Decoder over the provided [io.Reader]
|
||||
func NewDecoder(r io.Reader) *Decoder {
|
||||
rc := core.NewReadCloser(r)
|
||||
switch {
|
||||
case rc == nil:
|
||||
return nil
|
||||
default:
|
||||
dec := &Decoder{
|
||||
p: parser.NewParser(rc),
|
||||
Closer: rc,
|
||||
}
|
||||
return dec
|
||||
}
|
||||
}
|
||||
|
||||
// NewDecoderBytes creates a Decoder over a provided bytes array
|
||||
func NewDecoderBytes(b []byte) *Decoder {
|
||||
return NewDecoder(bytes.NewReader(b))
|
||||
}
|
||||
|
||||
// NewDecoderString creates a Decoder over a provided string of data
|
||||
func NewDecoderString(s string) *Decoder {
|
||||
return NewDecoder(strings.NewReader(s))
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package ini
|
||||
|
||||
import "io"
|
||||
|
||||
// ReadInto ...
|
||||
func ReadInto(v any, r io.Reader) error {
|
||||
dec := NewDecoder(r)
|
||||
|
||||
return dec.Unmarshal(v)
|
||||
}
|
||||
|
||||
// Unmarshal ...
|
||||
func (dec *Decoder) Unmarshal(any) error {
|
||||
return dec.p.Run()
|
||||
}
|
||||
Reference in New Issue
Block a user