|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.bundle; |
| 16 | + |
| 17 | +import com.google.common.collect.ImmutableMap; |
| 18 | +import org.yaml.snakeyaml.DumperOptions; |
| 19 | +import org.yaml.snakeyaml.Yaml; |
| 20 | +import org.yaml.snakeyaml.nodes.Node; |
| 21 | +import org.yaml.snakeyaml.representer.Represent; |
| 22 | +import org.yaml.snakeyaml.representer.Representer; |
| 23 | + |
| 24 | +/** Serializes a CelEnvironment into a YAML file. */ |
| 25 | +public final class CelEnvironmentYamlSerializer extends Representer { |
| 26 | + |
| 27 | + private static DumperOptions initDumperOptions() { |
| 28 | + DumperOptions options = new DumperOptions(); |
| 29 | + options.setIndent(2); |
| 30 | + options.setPrettyFlow(true); |
| 31 | + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
| 32 | + return options; |
| 33 | + } |
| 34 | + |
| 35 | + private static final DumperOptions YAML_OPTIONS = initDumperOptions(); |
| 36 | + |
| 37 | + private static final CelEnvironmentYamlSerializer INSTANCE = new CelEnvironmentYamlSerializer(); |
| 38 | + |
| 39 | + private CelEnvironmentYamlSerializer() { |
| 40 | + super(YAML_OPTIONS); |
| 41 | + this.multiRepresenters.put(CelEnvironment.class, new RepresentCelEnvironment()); |
| 42 | + this.multiRepresenters.put(CelEnvironment.VariableDecl.class, new RepresentVariableDecl()); |
| 43 | + this.multiRepresenters.put(CelEnvironment.FunctionDecl.class, new RepresentFunctionDecl()); |
| 44 | + this.multiRepresenters.put(CelEnvironment.OverloadDecl.class, new RepresentOverloadDecl()); |
| 45 | + this.multiRepresenters.put(CelEnvironment.TypeDecl.class, new RepresentTypeDecl()); |
| 46 | + this.multiRepresenters.put( |
| 47 | + CelEnvironment.ExtensionConfig.class, new RepresentExtensionConfig()); |
| 48 | + } |
| 49 | + |
| 50 | + public static String toYaml(CelEnvironment environment) { |
| 51 | + // Yaml is not thread-safe, so we create a new instance for each serialization. |
| 52 | + Yaml yaml = new Yaml(INSTANCE, YAML_OPTIONS); |
| 53 | + return yaml.dump(environment); |
| 54 | + } |
| 55 | + |
| 56 | + private final class RepresentCelEnvironment implements Represent { |
| 57 | + @Override |
| 58 | + public Node representData(Object data) { |
| 59 | + CelEnvironment environment = (CelEnvironment) data; |
| 60 | + ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>(); |
| 61 | + configMap.put("name", environment.name()); |
| 62 | + if (!environment.description().isEmpty()) { |
| 63 | + configMap.put("description", environment.description()); |
| 64 | + } |
| 65 | + if (!environment.container().isEmpty()) { |
| 66 | + configMap.put("container", environment.container()); |
| 67 | + } |
| 68 | + if (!environment.extensions().isEmpty()) { |
| 69 | + configMap.put("extensions", environment.extensions().asList()); |
| 70 | + } |
| 71 | + if (!environment.variables().isEmpty()) { |
| 72 | + configMap.put("variables", environment.variables().asList()); |
| 73 | + } |
| 74 | + if (!environment.functions().isEmpty()) { |
| 75 | + configMap.put("functions", environment.functions().asList()); |
| 76 | + } |
| 77 | + return represent(configMap.buildOrThrow()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private final class RepresentExtensionConfig implements Represent { |
| 82 | + @Override |
| 83 | + public Node representData(Object data) { |
| 84 | + CelEnvironment.ExtensionConfig extension = (CelEnvironment.ExtensionConfig) data; |
| 85 | + ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>(); |
| 86 | + configMap.put("name", extension.name()); |
| 87 | + if (extension.version() > 0 && extension.version() != Integer.MAX_VALUE) { |
| 88 | + configMap.put("version", extension.version()); |
| 89 | + } |
| 90 | + return represent(configMap.buildOrThrow()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private final class RepresentVariableDecl implements Represent { |
| 95 | + @Override |
| 96 | + public Node representData(Object data) { |
| 97 | + CelEnvironment.VariableDecl variable = (CelEnvironment.VariableDecl) data; |
| 98 | + ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>(); |
| 99 | + configMap.put("name", variable.name()).put("type_name", variable.type().name()); |
| 100 | + if (!variable.type().params().isEmpty()) { |
| 101 | + configMap.put("params", variable.type().params()); |
| 102 | + } |
| 103 | + return represent(configMap.buildOrThrow()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private final class RepresentFunctionDecl implements Represent { |
| 108 | + @Override |
| 109 | + public Node representData(Object data) { |
| 110 | + CelEnvironment.FunctionDecl function = (CelEnvironment.FunctionDecl) data; |
| 111 | + ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>(); |
| 112 | + configMap.put("name", function.name()).put("overloads", function.overloads().asList()); |
| 113 | + return represent(configMap.buildOrThrow()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private final class RepresentOverloadDecl implements Represent { |
| 118 | + @Override |
| 119 | + public Node representData(Object data) { |
| 120 | + CelEnvironment.OverloadDecl overload = (CelEnvironment.OverloadDecl) data; |
| 121 | + ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>(); |
| 122 | + configMap.put("id", overload.id()); |
| 123 | + if (overload.target().isPresent()) { |
| 124 | + configMap.put("target", overload.target().get()); |
| 125 | + } |
| 126 | + configMap.put("args", overload.arguments()).put("return", overload.returnType()); |
| 127 | + return represent(configMap.buildOrThrow()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private final class RepresentTypeDecl implements Represent { |
| 132 | + @Override |
| 133 | + public Node representData(Object data) { |
| 134 | + CelEnvironment.TypeDecl type = (CelEnvironment.TypeDecl) data; |
| 135 | + ImmutableMap.Builder<String, Object> configMap = new ImmutableMap.Builder<>(); |
| 136 | + configMap.put("type_name", type.name()); |
| 137 | + if (!type.params().isEmpty()) { |
| 138 | + configMap.put("params", type.params()); |
| 139 | + } |
| 140 | + if (type.isTypeParam()) { |
| 141 | + configMap.put("is_type_param", type.isTypeParam()); |
| 142 | + } |
| 143 | + return represent(configMap.buildOrThrow()); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments