Search in sources :

Example 1 with Collections2.transform

use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Collections2.transform in project beam by apache.

the class PipelineOptionsFactoryTest method testDescribe.

@Test
public void testDescribe() {
    List<PipelineOptionDescriptor> described = PipelineOptionsFactory.describe(Sets.newHashSet(PipelineOptions.class, TestDescribeOptions.class));
    Map<String, PipelineOptionDescriptor> mapped = uniqueIndex(described, input -> input.getName());
    assertEquals("no duplicates", described.size(), mapped.size());
    Collection<PipelineOptionDescriptor> filtered = Collections2.filter(described, input -> input.getGroup().equals(TestDescribeOptions.class.getName()));
    assertEquals(6, filtered.size());
    mapped = uniqueIndex(filtered, input -> input.getName());
    PipelineOptionDescriptor listDesc = mapped.get("list");
    assertThat(listDesc, notNullValue());
    assertThat(listDesc.getDescription(), isEmptyString());
    assertEquals(PipelineOptionType.Enum.ARRAY, listDesc.getType());
    assertThat(listDesc.getDefaultValue(), isEmptyString());
    PipelineOptionDescriptor stringDesc = mapped.get("string");
    assertThat(stringDesc, notNullValue());
    assertThat(stringDesc.getDescription(), isEmptyString());
    assertEquals(PipelineOptionType.Enum.STRING, stringDesc.getType());
    assertThat(stringDesc.getDefaultValue(), isEmptyString());
    PipelineOptionDescriptor integerDesc = mapped.get("integer");
    assertThat(integerDesc, notNullValue());
    assertEquals("integer property", integerDesc.getDescription());
    assertEquals(PipelineOptionType.Enum.INTEGER, integerDesc.getType());
    assertThat(integerDesc.getDefaultValue(), isEmptyString());
    PipelineOptionDescriptor floatDesc = mapped.get("float");
    assertThat(integerDesc, notNullValue());
    assertEquals("float number property", floatDesc.getDescription());
    assertEquals(PipelineOptionType.Enum.NUMBER, floatDesc.getType());
    assertThat(floatDesc.getDefaultValue(), isEmptyString());
    PipelineOptionDescriptor booleanSimpleDesc = mapped.get("boolean_simple");
    assertThat(booleanSimpleDesc, notNullValue());
    assertEquals("simple boolean property", booleanSimpleDesc.getDescription());
    assertEquals(PipelineOptionType.Enum.BOOLEAN, booleanSimpleDesc.getType());
    assertThat(booleanSimpleDesc.getDefaultValue(), equalTo("true"));
    PipelineOptionDescriptor booleanWrapperDesc = mapped.get("boolean_wrapper");
    assertThat(booleanWrapperDesc, notNullValue());
    assertThat(booleanWrapperDesc.getDescription(), isEmptyString());
    assertEquals(PipelineOptionType.Enum.BOOLEAN, booleanWrapperDesc.getType());
    assertThat(booleanWrapperDesc.getDefaultValue(), equalTo("false"));
}
Also used : Module(com.fasterxml.jackson.databind.Module) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ExpectedLogs(org.apache.beam.sdk.testing.ExpectedLogs) PipelineResult(org.apache.beam.sdk.PipelineResult) Matchers.not(org.hamcrest.Matchers.not) PipelineOptionDescriptor(org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionDescriptor) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) Sets(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Sets) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) ArrayListMultimap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ArrayListMultimap) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Collection(java.util.Collection) Set(java.util.Set) InterceptingUrlClassLoader(org.apache.beam.sdk.testing.InterceptingUrlClassLoader) List(java.util.List) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) RestoreSystemProperties(org.apache.beam.sdk.testing.RestoreSystemProperties) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) ListMultimap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ListMultimap) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Matchers.containsString(org.hamcrest.Matchers.containsString) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestRule(org.junit.rules.TestRule) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Assert.assertThrows(org.junit.Assert.assertThrows) Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) Maps.uniqueIndex(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps.uniqueIndex) Assert.assertSame(org.junit.Assert.assertSame) Collections2(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Collections2) PipelineRunner(org.apache.beam.sdk.PipelineRunner) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) PipelineOptionType(org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionType) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ROOT(java.util.Locale.ROOT) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Pipeline(org.apache.beam.sdk.Pipeline) ExpectedException(org.junit.rules.ExpectedException) StdSerializer(com.fasterxml.jackson.databind.ser.std.StdSerializer) PrintStream(java.io.PrintStream) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) PipelineRunnerRegistrar(org.apache.beam.sdk.runners.PipelineRunnerRegistrar) SystemUtils(org.apache.commons.lang3.SystemUtils) Charsets(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Charsets) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) CrashingRunner(org.apache.beam.sdk.testing.CrashingRunner) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) AutoService(com.google.auto.service.AutoService) Assert.assertEquals(org.junit.Assert.assertEquals) PipelineOptionDescriptor(org.apache.beam.model.jobmanagement.v1.JobApi.PipelineOptionDescriptor) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Test(org.junit.Test)

Example 2 with Collections2.transform

use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Collections2.transform in project beam by apache.

the class ByteBuddyUtils method createCollectionTransformFunction.

// When processing a container (e.g. List<T>) we need to recursively process the element type.
// This function
// generates a subclass of Function that can be used to recursively transform each element of the
// container.
static Class createCollectionTransformFunction(Type fromType, Type toType, Function<StackManipulation, StackManipulation> convertElement) {
    // Generate a TypeDescription for the class we want to generate.
    TypeDescription.Generic functionGenericType = TypeDescription.Generic.Builder.parameterizedType(Function.class, Primitives.wrap((Class) fromType), Primitives.wrap((Class) toType)).build();
    DynamicType.Builder<Function> builder = (DynamicType.Builder<Function>) BYTE_BUDDY.with(new InjectPackageStrategy((Class) fromType)).subclass(functionGenericType).method(ElementMatchers.named("apply")).intercept(new Implementation() {

        @Override
        public ByteCodeAppender appender(Target target) {
            return (methodVisitor, implementationContext, instrumentedMethod) -> {
                // this + method parameters.
                int numLocals = 1 + instrumentedMethod.getParameters().size();
                StackManipulation readValue = MethodVariableAccess.REFERENCE.loadFrom(1);
                StackManipulation stackManipulation = new StackManipulation.Compound(convertElement.apply(readValue), MethodReturn.REFERENCE);
                StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
                return new ByteCodeAppender.Size(size.getMaximalSize(), numLocals);
            };
        }

        @Override
        public InstrumentedType prepare(InstrumentedType instrumentedType) {
            return instrumentedType;
        }
    });
    return builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(((Class) fromType).getClassLoader()), ClassLoadingStrategy.Default.INJECTION).getLoaded();
}
Also used : Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) ArrayAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayAccess) TypeCasting(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.TypeCasting) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) ArrayFactory(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayFactory) ByteBuffer(java.nio.ByteBuffer) Opcodes(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Opcodes) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) Label(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Label) ClassUtils(org.apache.commons.lang3.ClassUtils) MethodVisitor(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.MethodVisitor) NullConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.NullConstant) Map(java.util.Map) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) MethodReturn(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodReturn) Method(java.lang.reflect.Method) Internal(org.apache.beam.sdk.annotations.Internal) ClassLoadingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.loading.ClassLoadingStrategy) TypeCreation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.TypeCreation) Collection(java.util.Collection) Set(java.util.Set) ReadableInstant(org.joda.time.ReadableInstant) Objects(java.util.Objects) List(java.util.List) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) BaseLocal(org.joda.time.base.BaseLocal) Context(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation.Context) Typing(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.Assigner.Typing) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) SortedMap(java.util.SortedMap) ByteBuddy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy) TypeDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription) Preconditions.checkNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) RandomString(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.utility.RandomString) MethodVariableAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodVariableAccess) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) ArrayUtils(org.apache.commons.lang3.ArrayUtils) NamingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.NamingStrategy) Constructor(java.lang.reflect.Constructor) FieldAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.FieldAccess) ArrayList(java.util.ArrayList) TypeParameter(org.apache.beam.sdk.values.TypeParameter) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType) Collections2(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Collections2) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) Parameter(java.lang.reflect.Parameter) Maps(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) IntegerConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.IntegerConstant) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Assigner(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.Assigner) Duplication(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Duplication) Compound(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation.Compound) ElementMatchers(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.matcher.ElementMatchers) Function(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Function) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) ClassWriter(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.ClassWriter) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) MethodInvocation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodInvocation) ForLoadedConstructor(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedConstructor) Instant(org.joda.time.Instant) ReadablePartial(org.joda.time.ReadablePartial) FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) Collections(java.util.Collections) BaseNameResolver(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.NamingStrategy.SuffixingRandom.BaseNameResolver) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) Primitives(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.Primitives) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) Compound(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation.Compound) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) Function(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Function) TypeDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType)

Aggregations

Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 ReflectHelpers (org.apache.beam.sdk.util.common.ReflectHelpers)2 Collections2 (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Collections2)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 Module (com.fasterxml.jackson.databind.Module)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1