All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.cel.common.CelDescriptors Maven / Gradle / Ivy

The newest version!
// Copyright 2022 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.common;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import java.util.Arrays;

/** Value object containing multiple set of descriptors to be provided into the CEL environment. */
@AutoValue
public abstract class CelDescriptors {

  /** Descriptors of type loaded from {@link FileDescriptor#getMessageTypes()}. */
  public abstract ImmutableSet messageTypeDescriptors();

  public abstract ImmutableSet enumDescriptors();

  /**
   * Set of field descriptors that are part of a descriptor's extensions. Key: Containing type's
   * full name, Value: Extension's field descriptors
   */
  public abstract ImmutableMultimap extensionDescriptors();

  /** File descriptors that were used to load the message type, enum descriptors and extensions. */
  public abstract ImmutableSet fileDescriptors();

  public static Builder builder() {
    return new AutoValue_CelDescriptors.Builder();
  }

  /** Builder for configuring the CelDescriptors */
  @AutoValue.Builder
  public abstract static class Builder {

    abstract Builder setMessageTypeDescriptors(ImmutableSet value);

    abstract Builder setEnumDescriptors(ImmutableSet value);

    abstract Builder setExtensionDescriptors(ImmutableMultimap value);

    abstract Builder setFileDescriptors(ImmutableSet value);

    protected abstract ImmutableSet.Builder messageTypeDescriptorsBuilder();

    protected abstract ImmutableSet.Builder enumDescriptorsBuilder();

    protected abstract ImmutableMultimap.Builder
        extensionDescriptorsBuilder();

    protected abstract ImmutableSet.Builder fileDescriptorsBuilder();

    public abstract CelDescriptors build();

    @CanIgnoreReturnValue
    final Builder addMessageTypeDescriptors(Descriptor... descriptors) {
      addMessageTypeDescriptors(Arrays.asList(descriptors));
      return this;
    }

    @CanIgnoreReturnValue
    final Builder addMessageTypeDescriptors(Iterable descriptors) {
      messageTypeDescriptorsBuilder().addAll(descriptors);
      return this;
    }

    @CanIgnoreReturnValue
    final Builder addEnumDescriptors(EnumDescriptor... enumDescriptors) {
      addEnumDescriptors(Arrays.asList(enumDescriptors));
      return this;
    }

    @CanIgnoreReturnValue
    final Builder addEnumDescriptors(Iterable enumDescriptors) {
      enumDescriptorsBuilder().addAll(enumDescriptors);
      return this;
    }

    @CanIgnoreReturnValue
    final Builder addExtensionDescriptors(Iterable extensionDescriptors) {
      extensionDescriptors.forEach(
          d -> extensionDescriptorsBuilder().put(d.getContainingType().getFullName(), d));
      return this;
    }

    @CanIgnoreReturnValue
    final Builder addFileDescriptors(FileDescriptor... fileDescriptors) {
      addFileDescriptors(Arrays.asList(fileDescriptors));
      return this;
    }

    @CanIgnoreReturnValue
    final Builder addFileDescriptors(Iterable fileDescriptors) {
      fileDescriptorsBuilder().addAll(fileDescriptors);
      return this;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy