1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-12 04:32:03 +00:00

Add ability to rename imported types

This commit is contained in:
Darren Shepherd 2017-12-22 23:16:28 -07:00
parent 57a671459b
commit 6d5879c98a
2 changed files with 24 additions and 4 deletions

View File

@ -26,13 +26,25 @@ var (
} }
) )
func (s *Schemas) TypeName(name string, obj interface{}) *Schemas {
s.typeNames[reflect.TypeOf(obj)] = name
return s
}
func (s *Schemas) getTypeName(t reflect.Type) string {
if name, ok := s.typeNames[t]; ok {
return name
}
return convert.LowerTitle(t.Name())
}
func (s *Schemas) AddMapperForType(version *APIVersion, obj interface{}, mapper ...Mapper) *Schemas { func (s *Schemas) AddMapperForType(version *APIVersion, obj interface{}, mapper ...Mapper) *Schemas {
if len(mapper) == 0 { if len(mapper) == 0 {
return s return s
} }
t := reflect.TypeOf(obj) t := reflect.TypeOf(obj)
typeName := convert.LowerTitle(t.Name()) typeName := s.getTypeName(t)
if len(mapper) == 1 { if len(mapper) == 1 {
return s.AddMapper(version, typeName, mapper[0]) return s.AddMapper(version, typeName, mapper[0])
} }
@ -111,7 +123,7 @@ func (s *Schemas) setupFilters(schema *Schema) {
} }
func (s *Schemas) MustCustomizeType(version *APIVersion, obj interface{}, f func(*Schema)) *Schemas { func (s *Schemas) MustCustomizeType(version *APIVersion, obj interface{}, f func(*Schema)) *Schemas {
name := convert.LowerTitle(reflect.TypeOf(obj).Name()) name := s.getTypeName(reflect.TypeOf(obj))
schema := s.Schema(version, name) schema := s.Schema(version, name)
if schema == nil { if schema == nil {
panic("Failed to find schema " + name) panic("Failed to find schema " + name)
@ -127,7 +139,7 @@ func (s *Schemas) MustCustomizeType(version *APIVersion, obj interface{}, f func
} }
func (s *Schemas) importType(version *APIVersion, t reflect.Type, overrides ...reflect.Type) (*Schema, error) { func (s *Schemas) importType(version *APIVersion, t reflect.Type, overrides ...reflect.Type) (*Schema, error) {
typeName := convert.LowerTitle(t.Name()) typeName := s.getTypeName(t)
existing := s.Schema(version, typeName) existing := s.Schema(version, typeName)
if existing != nil { if existing != nil {
@ -167,6 +179,7 @@ func (s *Schemas) importType(version *APIVersion, t reflect.Type, overrides ...r
mapper := &typeMapper{ mapper := &typeMapper{
Mappers: mappers, Mappers: mappers,
root: schema.CanList(),
} }
if err := mapper.ModifySchema(schema, s); err != nil { if err := mapper.ModifySchema(schema, s); err != nil {
@ -376,6 +389,8 @@ func getKeyValue(input string) (string, string) {
func (s *Schemas) determineSchemaType(version *APIVersion, t reflect.Type) (string, error) { func (s *Schemas) determineSchemaType(version *APIVersion, t reflect.Type) (string, error) {
switch t.Kind() { switch t.Kind() {
case reflect.Uint8:
return "byte", nil
case reflect.Bool: case reflect.Bool:
return "boolean", nil return "boolean", nil
case reflect.Int: case reflect.Int:
@ -397,6 +412,9 @@ func (s *Schemas) determineSchemaType(version *APIVersion, t reflect.Type) (stri
if err != nil { if err != nil {
return "", err return "", err
} }
if subType == "byte" {
return "base64", nil
}
return fmt.Sprintf("array[%s]", subType), nil return fmt.Sprintf("array[%s]", subType), nil
case reflect.String: case reflect.String:
return "string", nil return "string", nil

View File

@ -3,8 +3,8 @@ package types
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"reflect"
"strings" "strings"
"sync" "sync"
"github.com/rancher/norman/name" "github.com/rancher/norman/name"
@ -29,6 +29,7 @@ type BackReference struct {
type Schemas struct { type Schemas struct {
sync.Mutex sync.Mutex
typeNames map[reflect.Type]string
schemasByPath map[string]map[string]*Schema schemasByPath map[string]map[string]*Schema
schemasBySubContext map[string]*Schema schemasBySubContext map[string]*Schema
mappers map[string]map[string][]Mapper mappers map[string]map[string][]Mapper
@ -44,6 +45,7 @@ type Schemas struct {
func NewSchemas() *Schemas { func NewSchemas() *Schemas {
return &Schemas{ return &Schemas{
typeNames: map[reflect.Type]string{},
schemasByPath: map[string]map[string]*Schema{}, schemasByPath: map[string]map[string]*Schema{},
schemasBySubContext: map[string]*Schema{}, schemasBySubContext: map[string]*Schema{},
mappers: map[string]map[string][]Mapper{}, mappers: map[string]map[string][]Mapper{},