Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ require (
gopkg.in/yaml.v2 v2.2.1
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92
sigs.k8s.io/structured-merge-diff v0.0.0-20181214233322-d43a45b8663b
sigs.k8s.io/structured-merge-diff v0.0.0-20190416230737-b2ed7e1d99f6
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6 h1:4s3/R4+OYYYUKptXPhZKjQ04WJ6Eh
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92 h1:PgoMI/L1Nu5Vmvgm+vGheLuxKST8h6FMOqggyAFtHPc=
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
sigs.k8s.io/structured-merge-diff v0.0.0-20181214233322-d43a45b8663b h1:j8K3V+JoLLHALxzcnrPqiCwgSOzBJqFbMfjm17bs2kA=
sigs.k8s.io/structured-merge-diff v0.0.0-20181214233322-d43a45b8663b/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
sigs.k8s.io/structured-merge-diff v0.0.0-20190416230737-b2ed7e1d99f6 h1:HxGn4yiP+T5dFdwGo2WX/Jsi3OAjSuSkFLF/7QsshxI=
sigs.k8s.io/structured-merge-diff v0.0.0-20190416230737-b2ed7e1d99f6/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
46 changes: 35 additions & 11 deletions pkg/schemaconv/smd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ToSchema(models proto.Models) (*schema.Schema, error) {
if err := c.convertAll(); err != nil {
return nil, err
}

c.addCommonTypes()
return c.output, nil
}

Expand Down Expand Up @@ -95,14 +95,37 @@ func (c *convert) insertTypeDef(name string, model proto.Schema) {
c.output.Types = append(c.output.Types, def)
}

func (c *convert) addCommonTypes() {
c.output.Types = append(c.output.Types, untypedDef)
}

var untypedName string = "__untyped_atomic_"

var untypedDef schema.TypeDef = schema.TypeDef{
Name: untypedName,
Atom: schema.Atom{
Scalar: ptr(schema.Scalar("untyped")),
List: &schema.List{
ElementType: schema.TypeRef{
NamedType: &untypedName,
},
ElementRelationship: schema.Atomic,
},
Map: &schema.Map{
ElementType: schema.TypeRef{
NamedType: &untypedName,
},
ElementRelationship: schema.Atomic,
},
},
}

func (c *convert) makeRef(model proto.Schema) schema.TypeRef {
var tr schema.TypeRef
if r, ok := model.(*proto.Ref); ok {
if r.Reference() == "io.k8s.apimachinery.pkg.runtime.RawExtension" {
return schema.TypeRef{
Inlined: schema.Atom{
Untyped: &schema.Untyped{},
},
NamedType: &untypedName,
}
}
// reference a named type
Expand All @@ -116,7 +139,7 @@ func (c *convert) makeRef(model proto.Schema) schema.TypeRef {

if tr == (schema.TypeRef{}) {
// emit warning?
tr.Inlined.Untyped = &schema.Untyped{}
tr.NamedType = &untypedName
}
}
return tr
Expand Down Expand Up @@ -211,9 +234,10 @@ func (c *convert) VisitMap(m *proto.Map) {
// spec.
}

func ptr(s schema.Scalar) *schema.Scalar { return &s }

func (c *convert) VisitPrimitive(p *proto.Primitive) {
a := c.top()
ptr := func(s schema.Scalar) *schema.Scalar { return &s }
switch p.Type {
case proto.Integer:
a.Scalar = ptr(schema.Numeric)
Expand All @@ -227,21 +251,21 @@ func (c *convert) VisitPrimitive(p *proto.Primitive) {
// byte really means []byte and is encoded as a string.
a.Scalar = ptr(schema.String)
case "int-or-string":
a.Untyped = &schema.Untyped{}
a.Scalar = ptr(schema.Scalar("untyped"))
case "date-time":
a.Untyped = &schema.Untyped{}
a.Scalar = ptr(schema.Scalar("untyped"))
default:
a.Untyped = &schema.Untyped{}
a.Scalar = ptr(schema.Scalar("untyped"))
}
case proto.Boolean:
a.Scalar = ptr(schema.Boolean)
default:
a.Untyped = &schema.Untyped{}
a.Scalar = ptr(schema.Scalar("untyped"))
}
}

func (c *convert) VisitArbitrary(a *proto.Arbitrary) {
c.top().Untyped = &schema.Untyped{}
*c.top() = untypedDef.Atom
}

func (c *convert) VisitReference(proto.Reference) {
Expand Down
84 changes: 71 additions & 13 deletions pkg/schemaconv/testdata/new-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ types:
scalar: string
- name: data
type:
untyped: {}
namedType: __untyped_atomic_
- name: kind
type:
scalar: string
Expand Down Expand Up @@ -753,7 +753,7 @@ types:
scalar: string
- name: data
type:
untyped: {}
namedType: __untyped_atomic_
- name: kind
type:
scalar: string
Expand Down Expand Up @@ -1124,7 +1124,7 @@ types:
scalar: string
- name: data
type:
untyped: {}
namedType: __untyped_atomic_
- name: kind
type:
scalar: string
Expand Down Expand Up @@ -9342,7 +9342,15 @@ types:
type:
scalar: string
- name: io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.CustomResourceSubresourceStatus
untyped: {}
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
- name: io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.CustomResourceSubresources
struct:
fields:
Expand All @@ -9368,7 +9376,15 @@ types:
type:
scalar: string
- name: io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSON
untyped: {}
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
- name: io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps
struct:
fields:
Expand Down Expand Up @@ -9504,11 +9520,35 @@ types:
type:
scalar: boolean
- name: io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaPropsOrArray
untyped: {}
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
- name: io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaPropsOrBool
untyped: {}
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
- name: io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaPropsOrStringArray
untyped: {}
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
- name: io.k8s.apimachinery.pkg.api.resource.Quantity
scalar: string
- name: io.k8s.apimachinery.pkg.apis.meta.v1.APIGroup
Expand Down Expand Up @@ -9729,7 +9769,7 @@ types:
type:
scalar: string
- name: io.k8s.apimachinery.pkg.apis.meta.v1.MicroTime
untyped: {}
scalar: untyped
- name: io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta
struct:
fields:
Expand Down Expand Up @@ -9815,7 +9855,15 @@ types:
type:
scalar: string
- name: io.k8s.apimachinery.pkg.apis.meta.v1.Patch
untyped: {}
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
- name: io.k8s.apimachinery.pkg.apis.meta.v1.Preconditions
struct:
fields:
Expand Down Expand Up @@ -9895,13 +9943,13 @@ types:
type:
scalar: string
- name: io.k8s.apimachinery.pkg.apis.meta.v1.Time
untyped: {}
scalar: untyped
- name: io.k8s.apimachinery.pkg.apis.meta.v1.WatchEvent
struct:
fields:
- name: object
type:
untyped: {}
namedType: __untyped_atomic_
- name: type
type:
scalar: string
Expand All @@ -9912,7 +9960,7 @@ types:
type:
scalar: string
- name: io.k8s.apimachinery.pkg.util.intstr.IntOrString
untyped: {}
scalar: untyped
- name: io.k8s.apimachinery.pkg.version.Info
struct:
fields:
Expand Down Expand Up @@ -10139,3 +10187,13 @@ types:
- name: namespace
type:
scalar: string
- name: __untyped_atomic_
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic