Search in sources :

Example 1 with Oneof

use of com.google.api.tools.framework.model.Oneof in project toolkit by googleapis.

the class ProtoField method getOneofFieldsNames.

public static Iterable<Iterable<String>> getOneofFieldsNames(Iterable<FieldModel> fields, SurfaceNamer namer) {
    ImmutableSet.Builder<Oneof> oneOfsBuilder = ImmutableSet.builder();
    for (FieldModel field : fields) {
        Oneof oneof = ((ProtoField) field).protoField.getOneof();
        if (oneof == null) {
            continue;
        }
        oneOfsBuilder.add(oneof);
    }
    Iterable<Oneof> oneOfs = oneOfsBuilder.build();
    ImmutableList.Builder<Iterable<String>> fieldsNames = ImmutableList.builder();
    for (Oneof oneof : oneOfs) {
        boolean hasItems = false;
        ImmutableSet.Builder<String> fieldNames = ImmutableSet.builder();
        for (Field field : oneof.getFields()) {
            fieldNames.add(namer.getVariableName(new ProtoField(field)));
            hasItems = true;
        }
        if (hasItems) {
            fieldsNames.add(fieldNames.build());
        }
    }
    return fieldsNames.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Oneof(com.google.api.tools.framework.model.Oneof) Field(com.google.api.tools.framework.model.Field) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 2 with Oneof

use of com.google.api.tools.framework.model.Oneof in project toolkit by googleapis.

the class TestCaseTransformerTest method testResponseOneof.

@Test
public void testResponseOneof() {
    Oneof oneof1 = Mockito.mock(Oneof.class);
    Oneof oneof2 = Mockito.mock(Oneof.class);
    FieldModel field1 = Mockito.mock(FieldModel.class);
    Mockito.when(field1.isPrimitive()).thenReturn(true);
    Mockito.when(field1.isRepeated()).thenReturn(false);
    Mockito.when(field1.getOneof()).thenReturn(oneof1);
    FieldModel field2 = Mockito.mock(FieldModel.class);
    Mockito.when(field2.isPrimitive()).thenReturn(true);
    Mockito.when(field2.isRepeated()).thenReturn(false);
    Mockito.when(field2.getOneof()).thenReturn(oneof1);
    // not equal, even if they have the same properties; otherwise "containsExactly" below doesn't work.
    assertThat(field1).isNotEqualTo(field2);
    FieldModel field3 = Mockito.mock(FieldModel.class);
    Mockito.when(field3.isPrimitive()).thenReturn(true);
    Mockito.when(field3.isRepeated()).thenReturn(false);
    Mockito.when(field3.getOneof()).thenReturn(oneof2);
    List<FieldModel> fields;
    fields = Arrays.asList(field1);
    assertThat(TestCaseTransformer.responseInitFields(fields)).containsExactly(field1);
    // field1 and field2 have the same oneof, we can only choose one. First one wins.
    fields = Arrays.asList(field1, field2);
    assertThat(TestCaseTransformer.responseInitFields(fields)).containsExactly(field1);
    fields = Arrays.asList(field2, field1);
    assertThat(TestCaseTransformer.responseInitFields(fields)).containsExactly(field2);
    // field3 has a different oneof.
    fields = Arrays.asList(field1, field3);
    assertThat(TestCaseTransformer.responseInitFields(fields)).containsExactly(field1, field3);
    fields = Arrays.asList(field1, field2, field3);
    assertThat(TestCaseTransformer.responseInitFields(fields)).containsExactly(field1, field3);
}
Also used : Oneof(com.google.api.tools.framework.model.Oneof) FieldModel(com.google.api.codegen.config.FieldModel) Test(org.junit.Test)

Example 3 with Oneof

use of com.google.api.tools.framework.model.Oneof in project toolkit by googleapis.

the class FlatteningConfig method createFlattening.

/**
 * Creates an instance of FlatteningConfig based on a FlatteningGroupProto, linking it up with the
 * provided method.
 */
@Nullable
static FlatteningConfig createFlattening(DiagCollector diagCollector, ResourceNameMessageConfigs messageConfigs, ImmutableMap<String, ResourceNameConfig> resourceNameConfigs, MethodConfigProto methodConfigProto, FlatteningGroupProto flatteningGroup, MethodModel method) {
    boolean missing = false;
    ImmutableMap.Builder<String, FieldConfig> flattenedFieldConfigBuilder = ImmutableMap.builder();
    Set<String> oneofNames = new HashSet<>();
    for (String parameter : flatteningGroup.getParametersList()) {
        FieldModel parameterField = method.getInputField(parameter);
        if (parameterField == null) {
            diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Field missing for flattening: method = %s, message type = %s, field = %s", method.getFullName(), method.getInputFullName(), parameter));
            return null;
        }
        Oneof oneof = parameterField.getOneof();
        if (oneof != null) {
            String oneofName = oneof.getName();
            if (oneofNames.contains(oneofName)) {
                diagCollector.addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Value from oneof already specifed for flattening:%n" + "method = %s, message type = %s, oneof = %s", method.getFullName(), method.getInputFullName(), oneofName));
                return null;
            }
            oneofNames.add(oneofName);
        }
        ResourceNameTreatment defaultResourceNameTreatment = methodConfigProto.getResourceNameTreatment();
        if (!parameterField.mayBeInResourceName()) {
            defaultResourceNameTreatment = ResourceNameTreatment.NONE;
        }
        if (defaultResourceNameTreatment == null || defaultResourceNameTreatment.equals(ResourceNameTreatment.UNSET_TREATMENT)) {
            defaultResourceNameTreatment = ResourceNameTreatment.VALIDATE;
        }
        FieldConfig fieldConfig = FieldConfig.createFieldConfig(diagCollector, messageConfigs, methodConfigProto.getFieldNamePatternsMap(), resourceNameConfigs, parameterField, flatteningGroup.getParameterResourceNameTreatmentMap().get(parameter), defaultResourceNameTreatment);
        if (fieldConfig == null) {
            missing = true;
        } else {
            flattenedFieldConfigBuilder.put(parameter, fieldConfig);
        }
    }
    if (missing) {
        return null;
    }
    return new AutoValue_FlatteningConfig(flattenedFieldConfigBuilder.build(), flatteningGroup.getFlatteningGroupName());
}
Also used : Oneof(com.google.api.tools.framework.model.Oneof) ResourceNameTreatment(com.google.api.codegen.ResourceNameTreatment) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Aggregations

Oneof (com.google.api.tools.framework.model.Oneof)3 ResourceNameTreatment (com.google.api.codegen.ResourceNameTreatment)1 FieldModel (com.google.api.codegen.config.FieldModel)1 Field (com.google.api.tools.framework.model.Field)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 HashSet (java.util.HashSet)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1