|
| 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.runtime.planner; |
| 16 | + |
| 17 | + |
| 18 | +import com.google.common.collect.ImmutableList; |
| 19 | +import com.google.errorprone.annotations.Immutable; |
| 20 | +import dev.cel.common.types.CelTypeProvider; |
| 21 | +import dev.cel.common.types.TypeType; |
| 22 | +import dev.cel.runtime.GlobalResolver; |
| 23 | + |
| 24 | +@Immutable |
| 25 | +interface Attribute { |
| 26 | + Object resolve(GlobalResolver ctx); |
| 27 | + |
| 28 | + final class MaybeAttribute implements Attribute { |
| 29 | + private final ImmutableList<Attribute> attributes; |
| 30 | + |
| 31 | + @Override |
| 32 | + public Object resolve(GlobalResolver ctx) { |
| 33 | + for (Attribute attr : attributes) { |
| 34 | + Object value = attr.resolve(ctx); |
| 35 | + if (value != null) { |
| 36 | + return value; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // TODO: Handle unknowns |
| 41 | + throw new UnsupportedOperationException("Unknown attributes is not supported yet"); |
| 42 | + } |
| 43 | + |
| 44 | + MaybeAttribute(ImmutableList<Attribute> attributes) { |
| 45 | + this.attributes = attributes; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + final class NamespacedAttribute implements Attribute { |
| 50 | + private final ImmutableList<String> namespacedNames; |
| 51 | + private final CelTypeProvider typeProvider; |
| 52 | + |
| 53 | + @Override |
| 54 | + public Object resolve(GlobalResolver ctx) { |
| 55 | + for (String name : namespacedNames) { |
| 56 | + Object value = ctx.resolve(name); |
| 57 | + if (value != null) { |
| 58 | + // TODO: apply qualifiers |
| 59 | + return value; |
| 60 | + } |
| 61 | + |
| 62 | + TypeType type = typeProvider.findType(name).map(TypeType::create).orElse(null); |
| 63 | + if (type != null) { |
| 64 | + return type; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // TODO: Handle unknowns |
| 69 | + throw new UnsupportedOperationException("Unknown attributes is not supported yet"); |
| 70 | + } |
| 71 | + |
| 72 | + NamespacedAttribute(CelTypeProvider typeProvider, ImmutableList<String> namespacedNames) { |
| 73 | + this.typeProvider = typeProvider; |
| 74 | + this.namespacedNames = namespacedNames; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments