1 /* 2 * Copyright (c) 2018 Google, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.truth.extensions.proto; 18 19 import com.google.auto.value.AutoValue; 20 import com.google.common.collect.ImmutableList; 21 import com.google.protobuf.UnknownFieldSet; 22 import com.google.protobuf.WireFormat; 23 import java.util.List; 24 25 /** Convenience class encapsulating type information for unknown fields. */ 26 @AutoValue 27 abstract class UnknownFieldDescriptor { 28 29 enum Type { VARINT(WireFormat.WIRETYPE_VARINT)30 VARINT(WireFormat.WIRETYPE_VARINT) { 31 @Override 32 public List<?> getValues(UnknownFieldSet.Field field) { 33 return field.getVarintList(); 34 } 35 }, FIXED32(WireFormat.WIRETYPE_FIXED32)36 FIXED32(WireFormat.WIRETYPE_FIXED32) { 37 @Override 38 public List<?> getValues(UnknownFieldSet.Field field) { 39 return field.getFixed32List(); 40 } 41 }, FIXED64(WireFormat.WIRETYPE_FIXED64)42 FIXED64(WireFormat.WIRETYPE_FIXED64) { 43 @Override 44 public List<?> getValues(UnknownFieldSet.Field field) { 45 return field.getFixed64List(); 46 } 47 }, LENGTH_DELIMITED(WireFormat.WIRETYPE_LENGTH_DELIMITED)48 LENGTH_DELIMITED(WireFormat.WIRETYPE_LENGTH_DELIMITED) { 49 @Override 50 public List<?> getValues(UnknownFieldSet.Field field) { 51 return field.getLengthDelimitedList(); 52 } 53 }, GROUP(WireFormat.WIRETYPE_START_GROUP)54 GROUP(WireFormat.WIRETYPE_START_GROUP) { 55 @Override 56 public List<?> getValues(UnknownFieldSet.Field field) { 57 return field.getGroupList(); 58 } 59 }; 60 61 private static final ImmutableList<Type> TYPES = ImmutableList.copyOf(values()); 62 all()63 static ImmutableList<Type> all() { 64 return TYPES; 65 } 66 67 private final int wireType; 68 Type(int wireType)69 Type(int wireType) { 70 this.wireType = wireType; 71 } 72 73 /** Returns the corresponding values from the given field. */ getValues(UnknownFieldSet.Field field)74 abstract List<?> getValues(UnknownFieldSet.Field field); 75 76 /** Returns the {@link WireFormat} constant for this field type. */ wireType()77 final int wireType() { 78 return wireType; 79 } 80 } 81 create(int fieldNumber, Type type)82 static UnknownFieldDescriptor create(int fieldNumber, Type type) { 83 return new AutoValue_UnknownFieldDescriptor(fieldNumber, type); 84 } 85 fieldNumber()86 abstract int fieldNumber(); 87 type()88 abstract Type type(); 89 descriptors( int fieldNumber, UnknownFieldSet.Field field)90 static ImmutableList<UnknownFieldDescriptor> descriptors( 91 int fieldNumber, UnknownFieldSet.Field field) { 92 ImmutableList.Builder<UnknownFieldDescriptor> builder = ImmutableList.builder(); 93 for (Type type : Type.all()) { 94 if (!type.getValues(field).isEmpty()) { 95 builder.add(create(fieldNumber, type)); 96 } 97 } 98 return builder.build(); 99 } 100 } 101