2014-06-23 18:32:11 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-06-26 00:55:43 +00:00
|
|
|
package kubecfg
|
2014-06-12 19:37:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2014-09-01 05:10:49 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/apitools"
|
2014-06-12 19:37:35 +00:00
|
|
|
)
|
|
|
|
|
2014-08-13 22:10:02 +00:00
|
|
|
type Parser struct {
|
|
|
|
storageToType map[string]reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewParser(objectMap map[string]interface{}) *Parser {
|
|
|
|
typeMap := make(map[string]reflect.Type)
|
|
|
|
for name, obj := range objectMap {
|
|
|
|
typeMap[name] = reflect.TypeOf(obj)
|
|
|
|
}
|
|
|
|
return &Parser{typeMap}
|
2014-06-12 19:37:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 11:48:48 +00:00
|
|
|
// ToWireFormat takes input 'data' as either json or yaml, checks that it parses as the
|
|
|
|
// appropriate object type, and returns json for sending to the API or an error.
|
2014-08-13 22:10:02 +00:00
|
|
|
func (p *Parser) ToWireFormat(data []byte, storage string) ([]byte, error) {
|
|
|
|
prototypeType, found := p.storageToType[storage]
|
2014-06-12 19:37:35 +00:00
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("unknown storage type: %v", storage)
|
|
|
|
}
|
|
|
|
|
2014-06-13 00:11:02 +00:00
|
|
|
obj := reflect.New(prototypeType).Interface()
|
2014-09-01 05:10:49 +00:00
|
|
|
err := apitools.DecodeInto(data, obj)
|
2014-06-12 23:46:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-06-12 19:37:35 +00:00
|
|
|
}
|
2014-09-01 05:10:49 +00:00
|
|
|
return apitools.Encode(obj)
|
2014-06-12 19:37:35 +00:00
|
|
|
}
|
2014-07-23 00:25:06 +00:00
|
|
|
|
2014-08-13 22:10:02 +00:00
|
|
|
func (p *Parser) SupportedWireStorage() []string {
|
2014-07-23 00:25:06 +00:00
|
|
|
types := []string{}
|
2014-08-13 22:10:02 +00:00
|
|
|
for k := range p.storageToType {
|
2014-07-23 00:25:06 +00:00
|
|
|
types = append(types, k)
|
|
|
|
}
|
|
|
|
return types
|
|
|
|
}
|