hi Simon,
>> // Marshal returns the SCFG encoding of v.>> func Marshal(v interface{}) ([]byte, error) {>> Do we really need to use interface{} here? What is the use-case for passing a> Directive?> > More generally, I'd prefer to reserve Marshal/Encoder abstractions for> formatting scfg from arbitrary Go types, such as done in [1].> > [1]: https://lists.sr.ht/~emersion/public-inbox/patches/42271
I was mainly mirroring the encoding/json API.
It was also to (later on) be able to type-switch on types that implement
the SCFG (un)marshaling interface (or just use reflect).
but I didn't want to write that before the patch you mention was merged
into main :)
ie:
```go
type T1 struct {
Listen []string `scfg:"listen"`
}
type T2 struct {
Listen []string
}
func (t2 T2) MarshalSCFG() ([]byte, error) { ... }
var (
t1 T1
t2 T2
blk scfg.Block
enc = scfg.NewEncoder(os.Stdout)
)
err = enc.Encode(t1) // use reflect (TODO)
err = enc.Encode(t2) // use MarshalSCFG (TODO)
err = enc.Encode(blk) // ok
```
enc.Encode(blk) mirrors what can be done with:
```go
var (
msg = json.RawMessage(`{"hello":"world"}`)
err = json.NewEncoder(os.Stdout).Encode(msg)
)
```
[...]
>> // NewEncoder returns a new encoder that writes to w.>> func NewEncoder(w io.Writer) *Encoder {>> return &Encoder{>> w: w,>> ind: []byte(" "),>> Can we use tabs instead?
sure.
>> func (enc *Encoder) encodeDir(dir Directive) error {>> enc.whdr()>> enc.write([]byte(dir.Name))> I don't think writing the name is quite enough: we need to quote the string if> it contains special characters (DQUOTE, "\", LF, "'", "{", "}", WSP).
right.
v2 coming up.
-s
On Wednesday, October 4th, 2023 at 10:55, Sebastien Binet <s@sbinet.org> wrote:
> hi Simon,> > >> // Marshal returns the SCFG encoding of v.> >> func Marshal(v interface{}) ([]byte, error) {> >> > Do we really need to use interface{} here? What is the use-case for passing a> > Directive?> >> > More generally, I'd prefer to reserve Marshal/Encoder abstractions for> > formatting scfg from arbitrary Go types, such as done in [1].> >> > [1]: https://lists.sr.ht/~emersion/public-inbox/patches/42271> > I was mainly mirroring the encoding/json API.> > It was also to (later on) be able to type-switch on types that implement> the SCFG (un)marshaling interface (or just use reflect).> but I didn't want to write that before the patch you mention was merged> into main :)> > ie:> ```go> type T1 struct {> Listen []string `scfg:"listen"`> }> > type T2 struct {> Listen []string> }> > func (t2 T2) MarshalSCFG() ([]byte, error) { ... }> > var (> t1 T1> t2 T2> blk scfg.Block> enc = scfg.NewEncoder(os.Stdout)> )> > err = enc.Encode(t1) // use reflect (TODO)> err = enc.Encode(t2) // use MarshalSCFG (TODO)> err = enc.Encode(blk) // ok> ```> > enc.Encode(blk) mirrors what can be done with:> > ```go> var (> msg = json.RawMessage(`{"hello":"world"}`)> err = json.NewEncoder(os.Stdout).Encode(msg)> )> ```
That makes sense! That said, I'd still prefer to hold off exposing a public API
until we actually have a use for it. In other words, I'd prefer to only expose
Write for now, and add Marshal/Encoder when we actually implement the new
functionality.
> That makes sense! That said, I'd still prefer to hold off exposing a public API> until we actually have a use for it. In other words, I'd prefer to only expose> Write for now, and add Marshal/Encoder when we actually implement the new> functionality.
ok. I'll send a v3, then.