2323import dev .cel .expr .Decl .FunctionDecl .Overload ;
2424import com .google .auto .value .AutoValue ;
2525import com .google .common .collect .ArrayListMultimap ;
26- import com .google .common .collect .ImmutableList ;
2726import com .google .common .collect .ImmutableSet ;
2827import com .google .common .collect .ListMultimap ;
2928import com .google .errorprone .annotations .CanIgnoreReturnValue ;
@@ -97,7 +96,8 @@ public abstract class CelEnvironmentExporter {
9796 */
9897 abstract int maxExcludedStandardFunctionOverloads ();
9998
100- abstract ImmutableSet <CelExtensionLibrary > extensionLibraries ();
99+ abstract ImmutableSet <CelExtensionLibrary <? extends CelExtensionLibrary .FeatureSet >>
100+ extensionLibraries ();
101101
102102 /** Builder for {@link CelEnvironmentExporter}. */
103103 @ AutoValue .Builder
@@ -107,22 +107,21 @@ public abstract static class Builder {
107107
108108 public abstract Builder setMaxExcludedStandardFunctionOverloads (int count );
109109
110- abstract ImmutableSet .Builder <CelExtensionLibrary > extensionLibrariesBuilder ();
110+ abstract ImmutableSet .Builder <CelExtensionLibrary <? extends CelExtensionLibrary .FeatureSet >>
111+ extensionLibrariesBuilder ();
111112
112113 @ CanIgnoreReturnValue
113114 public Builder addStandardExtensions (CelOptions options ) {
114115 addExtensionLibraries (
115- CelExtensions .math (options , 0 ),
116- CelExtensions .math (options , 1 ),
117- CelExtensions .math (options , 2 ),
118- CelExtensions .lists (0 ),
119- CelExtensions .lists (1 ),
120- CelExtensions .lists (2 ));
116+ CelExtensions .getExtensionLibrary ("math" , options ),
117+ CelExtensions .getExtensionLibrary ("lists" , options ));
118+ // TODO: add support for remaining standard extensions
121119 return this ;
122120 }
123121
124122 @ CanIgnoreReturnValue
125- public Builder addExtensionLibraries (CelExtensionLibrary ... libraries ) {
123+ public Builder addExtensionLibraries (
124+ CelExtensionLibrary <? extends CelExtensionLibrary .FeatureSet >... libraries ) {
126125 extensionLibrariesBuilder ().add (libraries );
127126 return this ;
128127 }
@@ -216,52 +215,59 @@ public void visitMacro(CelMacro macro) {
216215 */
217216 private void addExtensionConfigsAndRemoveFromInventory (
218217 CelEnvironment .Builder envBuilder , Set <Object > inventory ) {
219- ImmutableList <CelExtensionLibrary > libraries =
220- ImmutableList .sortedCopyOf (
221- Comparator .comparing (CelExtensionLibrary ::getName )
222- .thenComparing (CelExtensionLibrary ::getVersion )
223- .reversed (),
224- extensionLibraries ());
218+ ArrayList <NamedFeatureSet > featureSets = new ArrayList <>();
219+
220+ for (CelExtensionLibrary <? extends CelExtensionLibrary .FeatureSet > extensionLibrary :
221+ extensionLibraries ()) {
222+ for (CelExtensionLibrary .FeatureSet featureSet : extensionLibrary .versions ()) {
223+ featureSets .add (NamedFeatureSet .create (extensionLibrary .name (), featureSet ));
224+ }
225+ }
226+
227+ featureSets .sort (
228+ Comparator .comparing (NamedFeatureSet ::name )
229+ .thenComparing (nfs -> nfs .featureSet ().version ())
230+ .reversed ());
225231
226232 Set <String > includedExtensions = new HashSet <>();
227- for (CelExtensionLibrary library : libraries ) {
228- if (includedExtensions .contains (library . getName ())) {
233+ for (NamedFeatureSet lib : featureSets ) {
234+ if (includedExtensions .contains (lib . name ())) {
229235 // We only need to infer the highest version library, so we can skip lower versions
230236 continue ;
231237 }
232238
233- if (checkIfExtensionIsIncludedAndRemoveFromInventory (inventory , library )) {
234- envBuilder .addExtensions (ExtensionConfig .of (library . getName (), library . getVersion ()));
235- includedExtensions .add (library . getName ());
239+ if (checkIfExtensionIsIncludedAndRemoveFromInventory (inventory , lib . featureSet () )) {
240+ envBuilder .addExtensions (ExtensionConfig .of (lib . name (), lib . featureSet (). version ()));
241+ includedExtensions .add (lib . name ());
236242 }
237243 }
238244 }
239245
240246 private boolean checkIfExtensionIsIncludedAndRemoveFromInventory (
241- Set <Object > inventory , CelExtensionLibrary library ) {
242- ImmutableSet <CelFunctionDecl > functions = library . getFunctions ();
243- ArrayList <Object > includedItems = new ArrayList <>(functions .size ());
247+ Set <Object > inventory , CelExtensionLibrary . FeatureSet featureSet ) {
248+ ImmutableSet <CelFunctionDecl > functions = featureSet . functions ();
249+ ArrayList <Object > includedFeatures = new ArrayList <>(functions .size ());
244250 for (CelFunctionDecl function : functions ) {
245251 for (CelOverloadDecl overload : function .overloads ()) {
246- NamedOverload item = NamedOverload .create (function .name (), overload );
247- if (!inventory .contains (item )) {
252+ NamedOverload feature = NamedOverload .create (function .name (), overload );
253+ if (!inventory .contains (feature )) {
248254 return false ;
249255 }
250- includedItems .add (item );
256+ includedFeatures .add (feature );
251257 }
252258 }
253259
254- ImmutableSet <CelMacro > macros = library . getMacros ();
260+ ImmutableSet <CelMacro > macros = featureSet . macros ();
255261 for (CelMacro macro : macros ) {
256262 if (!inventory .contains (macro )) {
257263 return false ;
258264 }
259- includedItems .add (macro );
265+ includedFeatures .add (macro );
260266 }
261267
262268 // TODO - Add checks for variables.
263269
264- inventory .removeAll (includedItems );
270+ inventory .removeAll (includedFeatures );
265271 return true ;
266272 }
267273
@@ -424,5 +430,18 @@ static NamedOverload create(String functionName, CelOverloadDecl overload) {
424430 return new AutoValue_CelEnvironmentExporter_NamedOverload (functionName , overload );
425431 }
426432 }
427- }
428433
434+ /**
435+ * Wrapper for CelExtensionLibrary.FeatureSet, associating it with the corresponding library name.
436+ */
437+ @ AutoValue
438+ abstract static class NamedFeatureSet {
439+ abstract String name ();
440+
441+ abstract CelExtensionLibrary .FeatureSet featureSet ();
442+
443+ static NamedFeatureSet create (String name , CelExtensionLibrary .FeatureSet featureSet ) {
444+ return new AutoValue_CelEnvironmentExporter_NamedFeatureSet (name , featureSet );
445+ }
446+ }
447+ }
0 commit comments