Refuse a calc over digits(0) or a repeat of 0, name an option's wrong kind as JSON does, type the name scope kinds, and compile only from a jsonvalue.Value

This commit is contained in:
2026-10-08 00:02:07 +02:00
committed by Lillem4n
parent ba2ae41ffe
commit 97e72988b1
13 changed files with 106 additions and 55 deletions
+3 -15
View File
@@ -2,8 +2,6 @@
package datafiles
import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"os"
@@ -11,6 +9,7 @@ import (
"strings"
"github.com/larvit/fejkdata/internal/grammar"
"github.com/larvit/fejkdata/internal/jsonvalue"
)
// Source is one tree to load: an fs.FS and the directory in it to start from. label
@@ -35,7 +34,7 @@ func FS(fsys fs.FS, base string) Source { return Source{fsys: fsys, base: base}
type Category struct {
Folders []string
Name string
JSON any
JSON jsonvalue.Value
rows *rowsFiles
}
@@ -142,7 +141,7 @@ func (s Source) compileFile(dir []string, file string, rows *rowsFiles, compile
if err != nil {
return false, fmt.Errorf("%s: %w", s.labelled(full), err)
}
raw, err := DecodeJSON(b)
raw, err := jsonvalue.Decode(b)
if err != nil {
return false, fmt.Errorf("%s: %w", s.labelled(full), err)
}
@@ -179,14 +178,3 @@ func (r *rowsFiles) read(name string) (string, error) {
}
return string(b), nil
}
// DecodeJSON decodes one JSON value, keeping each number as the json.Number it is written as.
func DecodeJSON(b []byte) (any, error) {
var v any
if !json.Valid(b) {
return nil, json.Unmarshal(b, &v)
}
dec := json.NewDecoder(bytes.NewReader(b))
dec.UseNumber()
return v, dec.Decode(&v)
}
+2 -2
View File
@@ -20,7 +20,7 @@ func walk(t *testing.T, src Source) ([]handed, error) {
t.Helper()
var got []handed
err := src.Walk(func(c Category) error {
got = append(got, handed{c.Folders, c.Name, c.JSON})
got = append(got, handed{c.Folders, c.Name, c.JSON.Any()})
return nil
})
return got, err
@@ -133,7 +133,7 @@ func TestLoadHandsOneCategoryAndItsRows(t *testing.T) {
var got handed
var rows string
err := src.Load([]string{"sub"}, "t", func(c Category) error {
got = handed{c.Folders, c.Name, c.JSON}
got = handed{c.Folders, c.Name, c.JSON.Any()}
var err error
rows, err = c.ReadRows("t.tsv")
return err