Skip to content

Commit

Permalink
handle custom decode
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTheElder committed Oct 15, 2019
1 parent 9f8ce52 commit 140c6a3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/container/cri/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
)

/*
Custom JSON / yaml (by way of json) serialization for these types
Custom JSON (de)serialization for these types
TODO: just use yaml ...
*/

// MarshalJSON implements custom encoding for JSON and Yaml
// MarshalJSON implements custom encoding for JSON
// https://golang.org/pkg/encoding/json/
func (m *Mount) MarshalJSON() ([]byte, error) {
type Alias Mount
Expand Down Expand Up @@ -67,7 +68,7 @@ func (m *Mount) UnmarshalJSON(data []byte) error {
return nil
}

// MarshalJSON implements custom encoding for JSON and Yaml
// MarshalJSON implements custom encoding for JSON
// https://golang.org/pkg/encoding/json/
func (p *PortMapping) MarshalJSON() ([]byte, error) {
type Alias PortMapping
Expand All @@ -84,7 +85,7 @@ func (p *PortMapping) MarshalJSON() ([]byte, error) {
})
}

// UnmarshalJSON implements custom decoding for JSON and Yaml
// UnmarshalJSON implements custom decoding for JSON
// https://golang.org/pkg/encoding/json/
func (p *PortMapping) UnmarshalJSON(data []byte) error {
type Alias PortMapping
Expand Down
68 changes: 68 additions & 0 deletions pkg/container/cri/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cri

/*
Custom YAML (de)serialization for these types
*/

// UnmarshalYAML implements custom decoding YAML
// https://godoc.org/gopkg.in/yaml.v3
func (m *Mount) UnmarshalYAML(unmarshal func(interface{}) error) error {
type Alias Mount
aux := &struct {
Propagation string `yaml:"propagation"`
*Alias
}{
Alias: (*Alias)(m),
}
if err := unmarshal(data, &aux); err != nil {
return err
}
// if unset, will fallback to the default (0)
if aux.Propagation != "" {
val, ok := MountPropagationNameToValue[aux.Propagation]
if !ok {
return fmt.Errorf("unknown propagation value: %s", aux.Propagation)
}
m.Propagation = MountPropagation(val)
}
return nil
}

// UnmarshalYAML implements custom decoding YAML
// https://godoc.org/gopkg.in/yaml.v3
func (p *PortMapping) UnmarshalYAML(unmarshal func(interface{}) error) error {
type Alias PortMapping
aux := &struct {
Protocol string `json:"protocol"`
*Alias
}{
Alias: (*Alias)(p),
}
if err := unmarshal(data, &aux); err != nil {
return err
}
if aux.Protocol != "" {
val, ok := PortMappingProtocolNameToValue[strings.ToUpper(aux.Protocol)]
if !ok {
return fmt.Errorf("unknown protocol value: %s", aux.Protocol)
}
p.Protocol = PortMappingProtocol(val)
}
return nil
}

0 comments on commit 140c6a3

Please sign in to comment.