mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-10 04:27:54 +00:00
allow conversions.Scheme to expose intermidiate versioned api object
This commit is contained in:
parent
5bd928fed0
commit
2906f85227
@ -22,31 +22,38 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (s *Scheme) DecodeToVersionedObject(data []byte) (obj interface{}, version, kind string, err error) {
|
||||
version, kind, err = s.DataVersionAndKind(data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if version == "" && s.InternalVersion != "" {
|
||||
return nil, "", "", fmt.Errorf("version not set in '%s'", string(data))
|
||||
}
|
||||
if kind == "" {
|
||||
return nil, "", "", fmt.Errorf("kind not set in '%s'", string(data))
|
||||
}
|
||||
obj, err = s.NewObject(version, kind)
|
||||
if err != nil {
|
||||
return nil, "", "", err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, obj); err != nil {
|
||||
return nil, "", "", err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Decode converts a JSON string back into a pointer to an api object.
|
||||
// Deduces the type based upon the fields added by the MetaInsertionFactory
|
||||
// technique. The object will be converted, if necessary, into the
|
||||
// s.InternalVersion type before being returned. Decode will not decode
|
||||
// objects without version set unless InternalVersion is also "".
|
||||
func (s *Scheme) Decode(data []byte) (interface{}, error) {
|
||||
version, kind, err := s.DataVersionAndKind(data)
|
||||
obj, version, kind, err := s.DecodeToVersionedObject(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if version == "" && s.InternalVersion != "" {
|
||||
return nil, fmt.Errorf("version not set in '%s'", string(data))
|
||||
}
|
||||
if kind == "" {
|
||||
return nil, fmt.Errorf("kind not set in '%s'", string(data))
|
||||
}
|
||||
obj, err := s.NewObject(version, kind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Version and Kind should be blank in memory.
|
||||
if err := s.SetVersionAndKind("", "", obj); err != nil {
|
||||
return nil, err
|
||||
|
Loading…
Reference in New Issue
Block a user