github.com/santhosh-tekuri/jsonschema/v6's init compiles every draft's metaschema (4, 6, 7, 2019-09, 2020-12 and their vocabularies): ~1.7 MB and ~21,000 allocations on every launch, the largest init left in the binary. magpie compiles a schema only for a Claude TUI answer tool, so `magpie statusline`, run on each Claude Code message, paid it for nothing. v6.0.3's non-test files are now in third_party/jsonschema (go.mod replace; LICENSE and MAGPIE.md beside them), with one change: init became loadDrafts behind a sync.Once, which Compiler.Compile calls first; loadDrafts compiles the metaschemas through the unexported compile exactly as init did. The Dockerfile copies third_party before `go mod download`, which needs the replace's directory. New TestAnswerSchemaCheckedAgainstItsDraft: three schemas that break their draft (type 5, minLength "x", required "a" under 2020-12) are refused. Broken by hand (Compile without ensureDrafts) it panics red. Measured on mote, sandbox HOME, t45 (v0.1.3522) against t46 (this): - GODEBUG=inittrace=1 sum: 2.98 MB / 28,515 allocs -> 1.28 MB / 7,385 - `magpie statusline`, 3 runs: instructions retired ~135M -> ~101M, peak memory footprint 15.5-16.1 MB -> 12.6-12.7 MB Run: upstream's own tests (`go test .`, GOWORK=off) on the forked draft.go and compiler.go: ok, as on upstream (36 pass; the official JSON-Schema-Test-Suite submodule isn't in the module zip, so it wasn't run). go vet ./... ok; GOOS=windows go vet ./internal/gateway ok; go test -run 'TestAnswer|TestClaudeTUISchema' ./internal/gateway ok; root TestNoEagerPackageRegexps, TestAllLazyPatternsCompile ok. Not tried: a docker build of the Dockerfile.
143 lines
2.4 KiB
Go
143 lines
2.4 KiB
Go
package jsonschema
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Position tells possible tokens in json.
|
|
type Position interface {
|
|
collect(v any, ptr jsonPointer) map[jsonPointer]any
|
|
}
|
|
|
|
// --
|
|
|
|
type AllProp struct{}
|
|
|
|
func (AllProp) collect(v any, ptr jsonPointer) map[jsonPointer]any {
|
|
obj, ok := v.(map[string]any)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
m := map[jsonPointer]any{}
|
|
for pname, pvalue := range obj {
|
|
m[ptr.append(pname)] = pvalue
|
|
}
|
|
return m
|
|
}
|
|
|
|
// --
|
|
|
|
type AllItem struct{}
|
|
|
|
func (AllItem) collect(v any, ptr jsonPointer) map[jsonPointer]any {
|
|
arr, ok := v.([]any)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
m := map[jsonPointer]any{}
|
|
for i, item := range arr {
|
|
m[ptr.append(strconv.Itoa(i))] = item
|
|
}
|
|
return m
|
|
}
|
|
|
|
// --
|
|
|
|
type Prop string
|
|
|
|
func (p Prop) collect(v any, ptr jsonPointer) map[jsonPointer]any {
|
|
obj, ok := v.(map[string]any)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
pvalue, ok := obj[string(p)]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return map[jsonPointer]any{
|
|
ptr.append(string(p)): pvalue,
|
|
}
|
|
}
|
|
|
|
// --
|
|
|
|
type Item int
|
|
|
|
func (i Item) collect(v any, ptr jsonPointer) map[jsonPointer]any {
|
|
arr, ok := v.([]any)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if i < 0 || int(i) >= len(arr) {
|
|
return nil
|
|
}
|
|
return map[jsonPointer]any{
|
|
ptr.append(strconv.Itoa(int(i))): arr[int(i)],
|
|
}
|
|
}
|
|
|
|
// --
|
|
|
|
// SchemaPath tells where to look for subschema inside keyword.
|
|
type SchemaPath []Position
|
|
|
|
func schemaPath(path string) SchemaPath {
|
|
var sp SchemaPath
|
|
for _, tok := range strings.Split(path, "/") {
|
|
var pos Position
|
|
switch tok {
|
|
case "*":
|
|
pos = AllProp{}
|
|
case "[]":
|
|
pos = AllItem{}
|
|
default:
|
|
if i, err := strconv.Atoi(tok); err == nil {
|
|
pos = Item(i)
|
|
} else {
|
|
pos = Prop(tok)
|
|
}
|
|
}
|
|
sp = append(sp, pos)
|
|
}
|
|
return sp
|
|
}
|
|
|
|
func (sp SchemaPath) collect(v any, ptr jsonPointer) map[jsonPointer]any {
|
|
if len(sp) == 0 {
|
|
return map[jsonPointer]any{
|
|
ptr: v,
|
|
}
|
|
}
|
|
p, sp := sp[0], sp[1:]
|
|
m := p.collect(v, ptr)
|
|
mm := map[jsonPointer]any{}
|
|
for ptr, v := range m {
|
|
m = sp.collect(v, ptr)
|
|
for k, v := range m {
|
|
mm[k] = v
|
|
}
|
|
}
|
|
return mm
|
|
}
|
|
|
|
func (sp SchemaPath) String() string {
|
|
var sb strings.Builder
|
|
for _, pos := range sp {
|
|
if sb.Len() != 0 {
|
|
sb.WriteByte('/')
|
|
}
|
|
switch pos := pos.(type) {
|
|
case AllProp:
|
|
sb.WriteString("*")
|
|
case AllItem:
|
|
sb.WriteString("[]")
|
|
case Prop:
|
|
sb.WriteString(string(pos))
|
|
case Item:
|
|
sb.WriteString(strconv.Itoa(int(pos)))
|
|
}
|
|
}
|
|
return sb.String()
|
|
}
|