0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-05-30 04:21:03 +02:00
gitea/modules/migration/file_format.go
Pascal Zimmermann ea723fe482
enhance: Migrate remaining gopkg.in/yaml.v3 usages to go.yaml.in/yaml/v4 (#37866)
### Description
Replaces all remaining direct `gopkg.in/yaml.v3` imports with
`go.yaml.in/yaml/v4` across models, modules, routers, services, and
integration tests. `gopkg.in/yaml.v3` moves from a direct to an indirect
dependency in `go.mod`.

#### API compatibility

The yaml.Node type, node.Kind/node.Content traversal style
(modules/markup/markdown/convertyaml.go), and the
UnmarshalYAML(*yaml.Node) interface signature
(modules/optional/serialization.go) are all preserved in v4 — no
call-site changes were required beyond the import path.

**Related:**
- https://github.com/go-gitea/gitea/pull/36564#issuecomment-4526536805

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Claude (Opus 4.8) <noreply@anthropic.com>
2026-05-29 01:12:11 +00:00

118 lines
2.3 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package migration
import (
"fmt"
"os"
"strings"
"time"
"gitea.dev/modules/json"
"gitea.dev/modules/log"
"github.com/santhosh-tekuri/jsonschema/v6"
"go.yaml.in/yaml/v4"
)
// schemaLoader implements jsonschema.URLLoader
type schemaLoader struct{}
func (l *schemaLoader) Load(url string) (any, error) {
return openSchema(url)
}
// Load project data from file, with optional validation
func Load(filename string, data any, validation bool) error {
isJSON := strings.HasSuffix(filename, ".json")
bs, err := os.ReadFile(filename)
if err != nil {
return err
}
if validation {
err := validate(bs, data, isJSON)
if err != nil {
return err
}
}
return unmarshal(bs, data, isJSON)
}
func unmarshal(bs []byte, data any, isJSON bool) error {
if isJSON {
return json.Unmarshal(bs, data)
}
return yaml.Unmarshal(bs, data)
}
func getSchema(filename string) (*jsonschema.Schema, error) {
c := jsonschema.NewCompiler()
c.UseLoader(&schemaLoader{})
return c.Compile(filename)
}
func validate(bs []byte, datatype any, isJSON bool) error {
var v any
err := unmarshal(bs, &v, isJSON)
if err != nil {
return err
}
if !isJSON {
v, err = toStringKeys(v)
if err != nil {
return err
}
}
var schemaFilename string
switch datatype := datatype.(type) {
case *[]*Issue:
schemaFilename = "issue.json"
case *[]*Milestone:
schemaFilename = "milestone.json"
default:
return fmt.Errorf("file_format:validate: %T has not a validation implemented", datatype)
}
sch, err := getSchema(schemaFilename)
if err != nil {
return err
}
err = sch.Validate(v)
if err != nil {
log.Error("migration validation with %s failed:\n%#v", schemaFilename, err)
}
return err
}
func toStringKeys(val any) (any, error) {
var err error
switch val := val.(type) {
case map[string]any:
m := make(map[string]any)
for k, v := range val {
m[k], err = toStringKeys(v)
if err != nil {
return nil, err
}
}
return m, nil
case []any:
l := make([]any, len(val))
for i, v := range val {
l[i], err = toStringKeys(v)
if err != nil {
return nil, err
}
}
return l, nil
case time.Time:
return val.Format(time.RFC3339), nil
default:
return val, nil
}
}