-
Notifications
You must be signed in to change notification settings - Fork 544
/
Copy pathserializer.ts
164 lines (145 loc) · 5.38 KB
/
serializer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { ObjectSerializer } from './api';
import { V1ObjectMeta } from './gen/model/v1ObjectMeta';
import { V1ListMeta } from './gen/model/v1ListMeta';
type AttributeType = {
name: string;
baseName: string;
type: string;
};
class KubernetesObject {
/**
* APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
*/
'apiVersion'?: string;
/**
* Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
*/
'kind'?: string;
'metadata'?: V1ObjectMeta;
static attributeTypeMap: AttributeType[] = [
{
name: 'apiVersion',
baseName: 'apiVersion',
type: 'string',
},
{
name: 'kind',
baseName: 'kind',
type: 'string',
},
{
name: 'metadata',
baseName: 'metadata',
type: 'V1ObjectMeta',
},
];
}
class KubernetesObjectList {
/**
* APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
*/
'apiVersion'?: string;
/**
* Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
*/
'kind'?: string;
'metadata'?: V1ListMeta;
'items'?: KubernetesObject[];
static attributeTypeMap: AttributeType[] = [
{
name: 'apiVersion',
baseName: 'apiVersion',
type: 'string',
},
{
name: 'kind',
baseName: 'kind',
type: 'string',
},
{
name: 'metadata',
baseName: 'metadata',
type: 'V1ListMeta',
},
];
}
const isKubernetesObject = (data: unknown): boolean =>
!!data && typeof data === 'object' && 'apiVersion' in data && 'kind' in data;
const isKubernetesObjectList = (data: any): boolean => isKubernetesObject(data) && 'items' in data;
const getClassOfKubernetesInstance = (data: any): any => {
if (!isKubernetesObject(data)) {
return null;
}
if (isKubernetesObjectList(data)) {
return KubernetesObjectList;
}
return KubernetesObject;
};
/**
* Wraps the ObjectSerializer to support custom resources and generic Kubernetes objects.
*/
class KubernetesObjectSerializer {
private static _instance: KubernetesObjectSerializer;
public static get instance(): KubernetesObjectSerializer {
if (this._instance) {
return this._instance;
}
this._instance = new KubernetesObjectSerializer();
return this._instance;
}
private constructor() {}
public serialize(data: any, type: string): any {
const obj = ObjectSerializer.serialize(data, type);
if (obj !== data) {
return obj;
}
if (!isKubernetesObject(data)) {
return obj;
}
const instance: Record<string, any> = {};
for (const attributeType of KubernetesObject.attributeTypeMap) {
instance[attributeType.name] = ObjectSerializer.serialize(
data[attributeType.baseName],
attributeType.type,
);
}
// add all unknown properties as is.
for (const [key, value] of Object.entries(data)) {
if (KubernetesObject.attributeTypeMap.find((t) => t.name === key)) {
continue;
}
instance[key] = value;
}
return instance;
}
public deserialize(data: any, type: string): any {
const obj = ObjectSerializer.deserialize(data, type);
if (obj !== data) {
// the serializer knows the type and already deserialized it.
return obj;
}
const kubernetesClass = getClassOfKubernetesInstance(data);
if (!kubernetesClass) {
return obj;
}
const instance = new kubernetesClass();
for (const attributeType of kubernetesClass.attributeTypeMap) {
instance[attributeType.name] = ObjectSerializer.deserialize(
data[attributeType.baseName],
attributeType.type,
);
}
// add all unknown properties as is.
for (const [key, value] of Object.entries(data)) {
if (KubernetesObject.attributeTypeMap.find((t) => t.name === key)) {
continue;
}
instance[key] = value;
}
if (instance.items) {
instance.items = instance.items.map((item: any) => this.deserialize(item, ''));
}
return instance;
}
}
export default KubernetesObjectSerializer.instance;