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.
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package jsonschema
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
)
|
|
|
|
// Decoder specifies how to decode specific contentEncoding.
|
|
type Decoder struct {
|
|
// Name of contentEncoding.
|
|
Name string
|
|
// Decode given string to byte array.
|
|
Decode func(string) ([]byte, error)
|
|
}
|
|
|
|
var decoders = map[string]*Decoder{
|
|
"base64": {
|
|
Name: "base64",
|
|
Decode: func(s string) ([]byte, error) {
|
|
return base64.StdEncoding.DecodeString(s)
|
|
},
|
|
},
|
|
}
|
|
|
|
// MediaType specified how to validate bytes against specific contentMediaType.
|
|
type MediaType struct {
|
|
// Name of contentMediaType.
|
|
Name string
|
|
|
|
// Validate checks whether bytes conform to this mediatype.
|
|
Validate func([]byte) error
|
|
|
|
// UnmarshalJSON unmarshals bytes into json value.
|
|
// This must be nil if this mediatype is not compatible
|
|
// with json.
|
|
UnmarshalJSON func([]byte) (any, error)
|
|
}
|
|
|
|
var mediaTypes = map[string]*MediaType{
|
|
"application/json": {
|
|
Name: "application/json",
|
|
Validate: func(b []byte) error {
|
|
var v any
|
|
return json.Unmarshal(b, &v)
|
|
},
|
|
UnmarshalJSON: func(b []byte) (any, error) {
|
|
return UnmarshalJSON(bytes.NewReader(b))
|
|
},
|
|
},
|
|
}
|