Skip to content

Commit 9c2ef9c

Browse files
authored
Fixes unsupported AST kind *ast.InterfaceType on the custon Object type (#139)
Signed-off-by: Spolti <filippespolti@gmail.com>
1 parent e5760e4 commit 9c2ef9c

File tree

4 files changed

+55
-113
lines changed

4 files changed

+55
-113
lines changed

hack/deepcopy-gen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ if [ "${GENS}" = "all" ] || grep -qw "deepcopy" <<<"${GENS}"; then
4141
echo "Generating deepcopy funcs"
4242
export GO111MODULE=on
4343
# for debug purposes, increase the log level by updating the -v flag to higher numbers, e.g. -v 4
44-
"${GOPATH}/bin/deepcopy-gen" -v 1 \
44+
"${GOPATH}/bin/deepcopy-gen" -v 2 \
4545
--input-dirs ./model -O zz_generated.deepcopy \
4646
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.txt" \
4747
"$@"

model/object.go

Lines changed: 46 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"math"
21-
"strconv"
22-
"strings"
2321
)
2422

2523
// Object is used to allow integration with DeepCopy tool by replacing 'interface' generic type.
@@ -32,126 +30,68 @@ import (
3230
// - Integer - holds int32 values, JSON marshal any number to float64 by default, during the marshaling process it is
3331
// parsed to int32
3432
// - raw - holds any not typed value, replaces the interface{} behavior.
33+
//
34+
// +kubebuilder:validation:Type=object
3535
type Object struct {
36-
IObject `json:",inline"`
37-
}
38-
39-
// IObject interface that can converted into one of the three subtypes
40-
type IObject interface {
41-
DeepCopyIObject() IObject
42-
}
43-
44-
// raw generic subtype
45-
type raw struct {
46-
IObject interface{}
36+
Type Type `json:",inline"`
37+
IntVal int32 `json:",inline"`
38+
StrVal string `json:",inline"`
39+
RawValue json.RawMessage `json:",inline"`
4740
}
4841

49-
func (o raw) DeepCopyIObject() IObject {
50-
return o
51-
}
42+
type Type int64
5243

53-
// Integer int32 type
54-
type Integer int
44+
const (
45+
Integer Type = iota
46+
String
47+
Raw
48+
)
5549

56-
func (m Integer) DeepCopyIObject() IObject {
57-
return m
50+
func FromInt(val int) Object {
51+
if val > math.MaxInt32 || val < math.MinInt32 {
52+
fmt.Println(fmt.Errorf("value: %d overflows int32", val))
53+
}
54+
return Object{Type: Integer, IntVal: int32(val)}
5855
}
5956

60-
// String string type
61-
type String string
62-
63-
func (m String) DeepCopyIObject() IObject {
64-
return m
57+
func FromString(val string) Object {
58+
return Object{Type: String, StrVal: val}
6559
}
6660

67-
// MarshalJSON marshal the given json object into the respective Object subtype.
68-
func (obj Object) MarshalJSON() ([]byte, error) {
69-
switch val := obj.IObject.(type) {
70-
case String:
71-
return []byte(fmt.Sprintf(`%q`, val)), nil
72-
case Integer:
73-
return []byte(fmt.Sprintf(`%d`, val)), nil
74-
case raw:
75-
custom, err := json.Marshal(&struct {
76-
raw
77-
}{
78-
val,
79-
})
80-
if err != nil {
81-
return nil, err
82-
}
83-
84-
// remove the field name and the last '}' for marshalling purposes
85-
st := strings.Replace(string(custom), "{\"IObject\":", "", 1)
86-
st = strings.TrimSuffix(st, "}")
87-
return []byte(st), nil
88-
default:
89-
return []byte(fmt.Sprintf("%+v", obj.IObject)), nil
61+
func FromRaw(val interface{}) Object {
62+
custom, err := json.Marshal(val)
63+
if err != nil {
64+
er := fmt.Errorf("failed to parse value to Raw: %w", err)
65+
fmt.Println(er.Error())
66+
return Object{}
9067
}
68+
return Object{Type: Raw, RawValue: custom}
9169
}
9270

9371
// UnmarshalJSON ...
9472
func (obj *Object) UnmarshalJSON(data []byte) error {
95-
var test interface{}
96-
if err := json.Unmarshal(data, &test); err != nil {
97-
return err
73+
if data[0] == '"' {
74+
obj.Type = String
75+
return json.Unmarshal(data, &obj.StrVal)
76+
} else if data[0] == '{' {
77+
obj.Type = Raw
78+
return json.Unmarshal(data, &obj.RawValue)
9879
}
99-
switch val := test.(type) {
100-
case string:
101-
var strVal String
102-
if err := json.Unmarshal(data, &strVal); err != nil {
103-
return err
104-
}
105-
obj.IObject = strVal
106-
return nil
107-
108-
case map[string]interface{}:
109-
var cstVal raw
110-
if err := json.Unmarshal(data, &cstVal.IObject); err != nil {
111-
return err
112-
}
113-
obj.IObject = cstVal
114-
return nil
115-
116-
default:
117-
// json parses all not typed numbers as float64, let's enforce to int32
118-
if valInt, parseErr := strconv.Atoi(fmt.Sprint(val)); parseErr != nil {
119-
return fmt.Errorf("falied to parse %d to int32: %w", valInt, parseErr)
120-
} else {
121-
var intVal Integer
122-
if err := json.Unmarshal(data, &intVal); err != nil {
123-
return err
124-
}
125-
obj.IObject = intVal
126-
return nil
127-
}
128-
}
129-
}
130-
131-
// FromInt creates an Object with an int32 value.
132-
func FromInt(val int) Object {
133-
if val > math.MaxInt32 || val < math.MinInt32 {
134-
panic(fmt.Errorf("value: %d overflows int32", val))
135-
}
136-
return Object{Integer(int32(val))}
137-
}
138-
139-
// FromString creates an Object with a string value.
140-
func FromString(val string) Object {
141-
return Object{String(val)}
80+
obj.Type = Integer
81+
return json.Unmarshal(data, &obj.IntVal)
14282
}
14383

144-
// FromRaw creates an Object with untyped values.
145-
func FromRaw(val interface{}) Object {
146-
var rawVal Object
147-
data, err := json.Marshal(val)
148-
if err != nil {
149-
panic(err)
150-
}
151-
var cstVal raw
152-
if err := json.Unmarshal(data, &cstVal.IObject); err != nil {
153-
panic(err)
84+
// MarshalJSON marshal the given json object into the respective Object subtype.
85+
func (obj Object) MarshalJSON() ([]byte, error) {
86+
switch obj.Type {
87+
case String:
88+
return []byte(fmt.Sprintf(`%q`, obj.StrVal)), nil
89+
case Integer:
90+
return []byte(fmt.Sprintf(`%d`, obj.IntVal)), nil
91+
case Raw:
92+
val, _ := json.Marshal(obj.RawValue)
93+
return val, nil
94+
default:
95+
return []byte(fmt.Sprintf("%+v", obj)), nil
15496
}
155-
rawVal.IObject = cstVal
156-
return rawVal
15797
}

model/parallel_state.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ type ParallelState struct {
5454
Timeouts *ParallelStateTimeout `json:"timeouts,omitempty"`
5555
}
5656

57-
func (s *ParallelState) DeepCopyState() State {
58-
return s
57+
func (ps *ParallelState) DeepCopyState() State {
58+
return ps
5959
}
6060

6161
type parallelStateForUnmarshal ParallelState
6262

6363
// UnmarshalJSON unmarshal ParallelState object from json bytes
64-
func (s *ParallelState) UnmarshalJSON(b []byte) error {
64+
func (ps *ParallelState) UnmarshalJSON(b []byte) error {
6565
if len(b) == 0 {
6666
// TODO: Normalize error messages
6767
return fmt.Errorf("no bytes to unmarshal")
@@ -75,7 +75,7 @@ func (s *ParallelState) UnmarshalJSON(b []byte) error {
7575
return err
7676
}
7777

78-
*s = ParallelState(*v)
78+
*ps = ParallelState(*v)
7979

8080
return nil
8181
}

model/zz_generated.deepcopy.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)