6 Commits

Author SHA1 Message Date
amery d4d9aab5d4 basic: GoString [WIP]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-01 14:26:52 +00:00
amery 7c19b74fd0 Unmarshal: WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-01 14:25:51 +00:00
amery 02c0978c6e Decoder: WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-01 14:25:51 +00:00
amery 845c24b20f build-sys: use local asciigoat.org/core [DO-NOT-MERGE]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-01 14:25:10 +00:00
amery a1e20fa3b6 basic: introduce Document.WriteTo() and Document.String()
producing an INI-style representation of the Document

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-01 14:22:48 +00:00
amery 174f72c4cf basic: introduce basic one-shot INI-style decoder
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-01 14:21:55 +00:00
4 changed files with 122 additions and 27 deletions
+22
View File
@@ -1,2 +1,24 @@
// Package basic provides a basic representation of dosini-style documents
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
}
+6 -4
View File
@@ -50,15 +50,17 @@ func (dec *decoder) execute(typ parser.TokenType) {
}
}
func (dec *decoder) addSection(name, key string, hadKey bool) {
func (dec *decoder) addSection(key, id string, allowEmptyID bool) {
emptyID := allowEmptyID && id == ""
// index for dec.current
n := len(dec.out.Sections)
// new section
dec.out.Sections = append(dec.out.Sections, Section{
Name: name,
Key: key,
HadKey: hadKey,
Key: key,
ID: id,
EmptyID: emptyID,
})
// pointer to the latest section
-23
View File
@@ -1,23 +0,0 @@
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
}
+94
View File
@@ -0,0 +1,94 @@
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
}
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)
}
// 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)
}
// GoString generates a string output for "%s"
func (doc *Document) String() string {
buf := doc.AsBuffer(WriteNewLine)
return buf.String()
}
// GoString generates a string output for "%#v"
func (Document) GoString() string {
var buf bytes.Buffer
return buf.String()
}