Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
4a6e6b3f
Commit
4a6e6b3f
authored
Mar 23, 2016
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #23213 from pweil-/generator-besteffort
Auto commit by PR queue bot
parents
050980b4
dd4ff169
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
2 deletions
+86
-2
conversion_generator.go
pkg/runtime/conversion_generator.go
+10
-2
conversion_generator_test.go
pkg/runtime/conversion_generator_test.go
+76
-0
No files found.
pkg/runtime/conversion_generator.go
View file @
4a6e6b3f
...
@@ -302,11 +302,15 @@ func (g *conversionGenerator) generateConversionsForSlice(inType, outType reflec
...
@@ -302,11 +302,15 @@ func (g *conversionGenerator) generateConversionsForSlice(inType, outType reflec
}
}
func
(
g
*
conversionGenerator
)
generateConversionsForStruct
(
inType
,
outType
reflect
.
Type
)
error
{
func
(
g
*
conversionGenerator
)
generateConversionsForStruct
(
inType
,
outType
reflect
.
Type
)
error
{
errs
:=
[]
string
{}
for
i
:=
0
;
i
<
inType
.
NumField
();
i
++
{
for
i
:=
0
;
i
<
inType
.
NumField
();
i
++
{
inField
:=
inType
.
Field
(
i
)
inField
:=
inType
.
Field
(
i
)
outField
,
found
:=
outType
.
FieldByName
(
inField
.
Name
)
outField
,
found
:=
outType
.
FieldByName
(
inField
.
Name
)
if
!
found
{
if
!
found
{
return
fmt
.
Errorf
(
"couldn't find a corresponding field %v in %v"
,
inField
.
Name
,
outType
)
// aggregate the errors so we can return them at the end but still provide
// best effort for generation for other fields in this type
errs
=
append
(
errs
,
fmt
.
Sprintf
(
"couldn't find a corresponding field %v in %v"
,
inField
.
Name
,
outType
))
continue
}
}
if
isComplexType
(
inField
.
Type
)
{
if
isComplexType
(
inField
.
Type
)
{
if
err
:=
g
.
generateConversionsBetween
(
inField
.
Type
,
outField
.
Type
);
err
!=
nil
{
if
err
:=
g
.
generateConversionsBetween
(
inField
.
Type
,
outField
.
Type
);
err
!=
nil
{
...
@@ -314,7 +318,11 @@ func (g *conversionGenerator) generateConversionsForStruct(inType, outType refle
...
@@ -314,7 +318,11 @@ func (g *conversionGenerator) generateConversionsForStruct(inType, outType refle
}
}
}
}
}
}
return
nil
if
len
(
errs
)
==
0
{
return
nil
}
return
fmt
.
Errorf
(
strings
.
Join
(
errs
,
","
))
}
}
// A buffer of lines that will be written.
// A buffer of lines that will be written.
...
...
pkg/runtime/conversion_generator_test.go
0 → 100644
View file @
4a6e6b3f
/*
Copyright 2016 The Kubernetes Authors 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.
*/
package
runtime
import
(
"reflect"
"testing"
"k8s.io/kubernetes/pkg/api/unversioned"
)
type
InternalSubtype
struct
{
String
string
}
type
Internal
struct
{
TypeMeta
Bool
bool
Complex
InternalSubtype
}
type
ExternalSubtype
struct
{
String
string
}
type
External
struct
{
TypeMeta
Complex
ExternalSubtype
}
func
(
obj
*
Internal
)
GetObjectKind
()
unversioned
.
ObjectKind
{
return
&
obj
.
TypeMeta
}
func
(
obj
*
External
)
GetObjectKind
()
unversioned
.
ObjectKind
{
return
&
obj
.
TypeMeta
}
func
TestGenerateConversionsForStruct
(
t
*
testing
.
T
)
{
internalGV
:=
unversioned
.
GroupVersion
{
Group
:
"test.group"
,
Version
:
APIVersionInternal
}
externalGV
:=
unversioned
.
GroupVersion
{
Group
:
"test.group"
,
Version
:
"external"
}
scheme
:=
NewScheme
()
scheme
.
Log
(
t
)
scheme
.
AddKnownTypeWithName
(
internalGV
.
WithKind
(
"Complex"
),
&
Internal
{})
scheme
.
AddKnownTypeWithName
(
externalGV
.
WithKind
(
"Complex"
),
&
External
{})
generator
:=
NewConversionGenerator
(
scheme
,
"foo"
)
typedGenerator
,
ok
:=
generator
.
(
*
conversionGenerator
)
if
!
ok
{
t
.
Fatalf
(
"error converting to conversionGenerator"
)
}
internalType
:=
reflect
.
TypeOf
(
Internal
{})
externalType
:=
reflect
.
TypeOf
(
External
{})
err
:=
typedGenerator
.
generateConversionsForStruct
(
internalType
,
externalType
)
if
err
==
nil
{
t
.
Errorf
(
"expected error for asymmetrical field"
)
}
// we are expecting Convert_runtime_InternalSubtype_To_runtime_ExternalSubtype to be generated
// even though the conversion for the parent type cannot be auto generated
if
len
(
typedGenerator
.
publicFuncs
)
!=
1
{
t
.
Errorf
(
"expected to find one public conversion for the Complex type but found: %v"
,
typedGenerator
.
publicFuncs
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment