forgejo/modules/optional/serialization.go
Earl Warren af5df243d4 chore: replace gopkg.in/yaml.v3 with go.yaml.in/yaml/v3 (#8956)
- gopkg.in/yaml.v3 is archived and unmaintained
- go.yaml.in/yaml/v3 is a compatible fork under the umbrella
  of https://yaml.org/

### Tests

There is no need for more tests than already provided: it is like an upgrade to a minor version, only from a fork. I browsed the changes and there are some bug fixes. They all seem reasonably minimal. It is not one of those forks that went crazy with breaking changes 😁 And there is a non zero chance that [a bug that matters to Forgejo Actions](https://github.com/yaml/go-yaml/issues/76) is fixed there. It is rare and can wait but it did happen on Codeberg.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8956
Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-08-20 15:31:12 +02:00

46 lines
792 B
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package optional
import (
"forgejo.org/modules/json"
"go.yaml.in/yaml/v3"
)
func (o *Option[T]) UnmarshalJSON(data []byte) error {
var v *T
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*o = FromPtr(v)
return nil
}
func (o Option[T]) MarshalJSON() ([]byte, error) {
if !o.Has() {
return []byte("null"), nil
}
return json.Marshal(o.Value())
}
func (o *Option[T]) UnmarshalYAML(value *yaml.Node) error {
var v *T
if err := value.Decode(&v); err != nil {
return err
}
*o = FromPtr(v)
return nil
}
func (o Option[T]) MarshalYAML() (any, error) {
if !o.Has() {
return nil, nil
}
value := new(yaml.Node)
err := value.Encode(o.Value())
return value, err
}