-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCelEnvironmentYamlSerializer.java
More file actions
146 lines (132 loc) · 5.89 KB
/
CelEnvironmentYamlSerializer.java
File metadata and controls
146 lines (132 loc) · 5.89 KB
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
// Copyright 2025 Google LLC
//
// 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
//
// https://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 dev.cel.bundle;
import com.google.common.collect.ImmutableMap;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;
/** Serializes a CelEnvironment into a YAML file. */
public final class CelEnvironmentYamlSerializer extends Representer {
private static DumperOptions initDumperOptions() {
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setPrettyFlow(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
return options;
}
private static final DumperOptions YAML_OPTIONS = initDumperOptions();
private static final CelEnvironmentYamlSerializer INSTANCE = new CelEnvironmentYamlSerializer();
private CelEnvironmentYamlSerializer() {
super(YAML_OPTIONS);
this.multiRepresenters.put(CelEnvironment.class, new RepresentCelEnvironment());
this.multiRepresenters.put(CelEnvironment.VariableDecl.class, new RepresentVariableDecl());
this.multiRepresenters.put(CelEnvironment.FunctionDecl.class, new RepresentFunctionDecl());
this.multiRepresenters.put(CelEnvironment.OverloadDecl.class, new RepresentOverloadDecl());
this.multiRepresenters.put(CelEnvironment.TypeDecl.class, new RepresentTypeDecl());
this.multiRepresenters.put(
CelEnvironment.ExtensionConfig.class, new RepresentExtensionConfig());
}
public static String toYaml(CelEnvironment environment) {
// Yaml is not thread-safe, so we create a new instance for each serialization.
Yaml yaml = new Yaml(INSTANCE, YAML_OPTIONS);
return yaml.dump(environment);
}
private final class RepresentCelEnvironment implements Represent {
@Override
public Node representData(Object data) {
CelEnvironment environment = (CelEnvironment) data;
ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>();
configMap.put("name", environment.name());
if (!environment.description().isEmpty()) {
configMap.put("description", environment.description());
}
if (!environment.container().isEmpty()) {
configMap.put("container", environment.container());
}
if (!environment.extensions().isEmpty()) {
configMap.put("extensions", environment.extensions().asList());
}
if (!environment.variables().isEmpty()) {
configMap.put("variables", environment.variables().asList());
}
if (!environment.functions().isEmpty()) {
configMap.put("functions", environment.functions().asList());
}
return represent(configMap.buildOrThrow());
}
}
private final class RepresentExtensionConfig implements Represent {
@Override
public Node representData(Object data) {
CelEnvironment.ExtensionConfig extension = (CelEnvironment.ExtensionConfig) data;
ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>();
configMap.put("name", extension.name());
if (extension.version() > 0 && extension.version() != Integer.MAX_VALUE) {
configMap.put("version", extension.version());
}
return represent(configMap.buildOrThrow());
}
}
private final class RepresentVariableDecl implements Represent {
@Override
public Node representData(Object data) {
CelEnvironment.VariableDecl variable = (CelEnvironment.VariableDecl) data;
ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>();
configMap.put("name", variable.name()).put("type_name", variable.type().name());
if (!variable.type().params().isEmpty()) {
configMap.put("params", variable.type().params());
}
return represent(configMap.buildOrThrow());
}
}
private final class RepresentFunctionDecl implements Represent {
@Override
public Node representData(Object data) {
CelEnvironment.FunctionDecl function = (CelEnvironment.FunctionDecl) data;
ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>();
configMap.put("name", function.name()).put("overloads", function.overloads().asList());
return represent(configMap.buildOrThrow());
}
}
private final class RepresentOverloadDecl implements Represent {
@Override
public Node representData(Object data) {
CelEnvironment.OverloadDecl overload = (CelEnvironment.OverloadDecl) data;
ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>();
configMap.put("id", overload.id());
if (overload.target().isPresent()) {
configMap.put("target", overload.target().get());
}
configMap.put("args", overload.arguments()).put("return", overload.returnType());
return represent(configMap.buildOrThrow());
}
}
private final class RepresentTypeDecl implements Represent {
@Override
public Node representData(Object data) {
CelEnvironment.TypeDecl type = (CelEnvironment.TypeDecl) data;
ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>();
configMap.put("type_name", type.name());
if (!type.params().isEmpty()) {
configMap.put("params", type.params());
}
if (type.isTypeParam()) {
configMap.put("is_type_param", type.isTypeParam());
}
return represent(configMap.buildOrThrow());
}
}
}