Compare commits
4 Commits
v0.2.1
..
ae5c095792
| Author | SHA1 | Date | |
|---|---|---|---|
| ae5c095792 | |||
| adcbd9d7a3 | |||
| 110b110feb | |||
| b9771fdc3b |
@@ -1,24 +1,2 @@
|
|||||||
// Package basic provides a basic representation of dosini-style documents
|
// Package basic provides a basic representation of dosini-style documents
|
||||||
package basic
|
package basic
|
||||||
|
|
||||||
// Document represents an INI-style document
|
|
||||||
type Document struct {
|
|
||||||
Global []Field
|
|
||||||
|
|
||||||
Sections []Section
|
|
||||||
}
|
|
||||||
|
|
||||||
// Section represents an INI-style section with optional GIT-style IDs
|
|
||||||
type Section struct {
|
|
||||||
Key string
|
|
||||||
ID string
|
|
||||||
EmptyID bool
|
|
||||||
|
|
||||||
Fields []Field
|
|
||||||
}
|
|
||||||
|
|
||||||
// Field represents a key = value entry in an INI-style document
|
|
||||||
type Field struct {
|
|
||||||
Key string
|
|
||||||
Value string
|
|
||||||
}
|
|
||||||
|
|||||||
+4
-6
@@ -50,17 +50,15 @@ func (dec *decoder) execute(typ parser.TokenType) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dec *decoder) addSection(key, id string, allowEmptyID bool) {
|
func (dec *decoder) addSection(name, key string, hadKey bool) {
|
||||||
emptyID := allowEmptyID && id == ""
|
|
||||||
|
|
||||||
// index for dec.current
|
// index for dec.current
|
||||||
n := len(dec.out.Sections)
|
n := len(dec.out.Sections)
|
||||||
|
|
||||||
// new section
|
// new section
|
||||||
dec.out.Sections = append(dec.out.Sections, Section{
|
dec.out.Sections = append(dec.out.Sections, Section{
|
||||||
Key: key,
|
Name: name,
|
||||||
ID: id,
|
Key: key,
|
||||||
EmptyID: emptyID,
|
HadKey: hadKey,
|
||||||
})
|
})
|
||||||
|
|
||||||
// pointer to the latest section
|
// pointer to the latest section
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
-104
@@ -1,104 +0,0 @@
|
|||||||
package basic
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"asciigoat.org/ini/parser"
|
|
||||||
)
|
|
||||||
|
|
||||||
// WriteNewLine is the new line representation used by [doc.WriteTo]
|
|
||||||
const WriteNewLine = "\n"
|
|
||||||
|
|
||||||
// AsBuffer returns a INI representation of the document on
|
|
||||||
// a memory buffer
|
|
||||||
func (doc *Document) AsBuffer(nl string) *bytes.Buffer {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
|
|
||||||
if len(doc.Global) > 0 {
|
|
||||||
_, _ = writeFieldsTo(&buf, doc.Global, nl)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, sec := range doc.Sections {
|
|
||||||
if buf.Len() > 0 {
|
|
||||||
_, _ = buf.WriteString(nl)
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = writeSectionToBuffer(&buf, &sec, nl)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &buf
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeFieldsTo(w io.Writer, fields []Field, nl string) (int64, error) {
|
|
||||||
var written int
|
|
||||||
for _, field := range fields {
|
|
||||||
n, err := fmt.Fprintf(w, "%s = %q%s", field.Key, field.Value, nl)
|
|
||||||
switch {
|
|
||||||
case err != nil:
|
|
||||||
return int64(written), err
|
|
||||||
case n > 0:
|
|
||||||
written += n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
var written, n int
|
|
||||||
|
|
||||||
_, _ = w.WriteRune(parser.RuneSectionStart)
|
|
||||||
written++
|
|
||||||
|
|
||||||
n, _ = w.WriteString(sec.Key)
|
|
||||||
written += n
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case sec.EmptyID:
|
|
||||||
n, _ = w.WriteString(" \"\"")
|
|
||||||
written += n
|
|
||||||
case sec.ID != "":
|
|
||||||
_, _ = w.WriteRune(' ')
|
|
||||||
n, _ = fmt.Fprintf(w, "%q", sec.ID)
|
|
||||||
written += n + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _ = w.WriteRune(parser.RuneSectionEnd)
|
|
||||||
written++
|
|
||||||
|
|
||||||
n, _ = w.WriteString(nl)
|
|
||||||
written += n
|
|
||||||
|
|
||||||
n64, _ := writeFieldsTo(w, sec.Fields, nl)
|
|
||||||
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
|
|
||||||
// onto the provided writer.
|
|
||||||
func (doc *Document) WriteTo(w io.Writer) (int64, error) {
|
|
||||||
buf := doc.AsBuffer(WriteNewLine)
|
|
||||||
return buf.WriteTo(w)
|
|
||||||
}
|
|
||||||
|
|
||||||
// String generates a string output for "%s"
|
|
||||||
func (doc *Document) String() string {
|
|
||||||
buf := doc.AsBuffer(WriteNewLine)
|
|
||||||
return buf.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))
|
||||||
|
}
|
||||||
@@ -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=
|
||||||
|
|||||||
@@ -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