[CHORE] Update jsonschema library to v6

- Update the `github.com/santhosh-tekuri/jsonschema` library from v5 to
v6.
- Update the migration loader function to a type, which is now required
in V6.
- `github.com/santhosh-tekuri/jsonschema/v6` was already used by gof3,
so removing the v5 library and using the v6 library reduces the binary
size of Forgejo.
  - Before: 95912040 bytes
  - After: 95706152 bytes
This commit is contained in:
Gusted 2024-07-14 16:54:09 +02:00
parent 6e83c39f13
commit 45401e044f
No known key found for this signature in database
GPG key ID: FD821B732837125F
8 changed files with 29 additions and 20 deletions

View file

@ -12,7 +12,7 @@ import (
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/santhosh-tekuri/jsonschema/v6"
"gopkg.in/yaml.v3"
)
@ -43,7 +43,7 @@ func unmarshal(bs []byte, data any, isJSON bool) error {
func getSchema(filename string) (*jsonschema.Schema, error) {
c := jsonschema.NewCompiler()
c.LoadURL = openSchema
c.UseLoader(&SchemaLoader{})
return c.Compile(filename)
}

View file

@ -7,7 +7,7 @@ import (
"strings"
"testing"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/santhosh-tekuri/jsonschema/v6"
"github.com/stretchr/testify/assert"
)

View file

@ -6,14 +6,17 @@
package migration
import (
"io"
"net/url"
"os"
"path"
"path/filepath"
"github.com/santhosh-tekuri/jsonschema/v6"
)
func openSchema(s string) (io.ReadCloser, error) {
type SchemaLoader struct{}
func (*SchemaLoader) Load(s string) (any, error) {
u, err := url.Parse(s)
if err != nil {
return nil, err
@ -34,5 +37,11 @@ func openSchema(s string) (io.ReadCloser, error) {
filename = filepath.Join("modules/migration/schemas", basename)
}
}
return os.Open(filename)
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return jsonschema.UnmarshalJSON(f)
}

View file

@ -6,10 +6,18 @@
package migration
import (
"io"
"path"
"github.com/santhosh-tekuri/jsonschema/v6"
)
func openSchema(filename string) (io.ReadCloser, error) {
return Assets.Open(path.Base(filename))
type SchemaLoader struct{}
func (*SchemaLoader) Load(filename string) (any, error) {
f, err := Assets.Open(path.Base(filename))
if err != nil {
return nil, err
}
defer f.Close()
return jsonschema.UnmarshalJSON(f)
}