forked from kubernetes-client/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer.ts
112 lines (102 loc) · 3.82 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
import { ObjectSerializer as InternalSerializer, V1ObjectMeta } from './gen/models/ObjectSerializer.js';
type AttributeType = {
name: string;
baseName: string;
type: string;
format: 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',
format: '',
},
{
name: 'kind',
baseName: 'kind',
type: 'string',
format: '',
},
{
name: 'metadata',
baseName: 'metadata',
type: 'V1ObjectMeta',
format: '',
},
];
}
const isKubernetesObject = (data: unknown): boolean =>
!!data && typeof data === 'object' && 'apiVersion' in data && 'kind' in data;
/**
* Wraps the ObjectSerializer to support custom resources and generic Kubernetes objects.
*/
export class ObjectSerializer extends InternalSerializer {
public static serialize(data: any, type: string, format: string = ''): any {
const obj = InternalSerializer.serialize(data, type, format);
if (obj !== data) {
return obj;
}
if (!isKubernetesObject(data)) {
return obj;
}
const instance: Record<string, any> = {};
for (const attributeType of KubernetesObject.attributeTypeMap) {
const value = data[attributeType.baseName];
if (value !== undefined) {
instance[attributeType.name] = InternalSerializer.serialize(
data[attributeType.baseName],
attributeType.type,
attributeType.format,
);
}
}
// 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 static deserialize(data: any, type: string, format: string = ''): any {
const obj = InternalSerializer.deserialize(data, type, format);
if (obj !== data) {
// the serializer knows the type and already deserialized it.
return obj;
}
if (!isKubernetesObject(data)) {
return obj;
}
const instance = new KubernetesObject();
for (const attributeType of KubernetesObject.attributeTypeMap) {
const value = data[attributeType.baseName];
if (value !== undefined) {
instance[attributeType.name] = InternalSerializer.deserialize(
data[attributeType.baseName],
attributeType.type,
attributeType.format,
);
}
}
// 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;
}
}