go-yaml / yaml

YAML support for the Go language.
Other
6.88k stars 1.05k forks source link

Custom unmarshalling with support for known fields #1049

Open ivankatliarchuk opened 1 month ago

ivankatliarchuk commented 1 month ago

I have something like this

in our code

func (t *TestJob) UnmarshalYAML(node *yaml.Node) error {
    type alias TestJob
    dataAlias := (*alias)(t)

    err := node.Decode(&dataAlias)
    if err != nil {
        return err
    }
    t.SetCapabilities()
    return nil
}

func Unmarshalling() {
  suite := TestSuite{
    chartRoute: chartRoute,
    fromRender: fromRender,
  }

  yamlDecoder := yaml.NewDecoder(strings.NewReader(content)) 
  yamlDecoder.KnownFields(true)

   if err := yamlDecoder.Decode(&suite); err != nil {
      return &suite, err
   }
}

Related https://github.com/go-yaml/yaml/issues/602#issuecomment-623485602 So there is no way to throw an error in custom unmarshaller if there is an unknown field.

ivankatliarchuk commented 1 month ago

Hi @niemeyer would you access a pull request or project is no longer maintained?