ProvidenceConfigUtil { @SuppressWarnings("unchecked") static Object asType(PDescriptor descriptor, Object o) throws ProvidenceConfigException { if (o == null) { return null; } switch (descriptor.getType()) { case BOOL: return asBoolean(o); case BYTE: return (byte) asInteger(o, Byte.MIN_VALUE, Byte.MAX_VALUE); case I16: return (short) asInteger(o, Short.MIN_VALUE, Short.MAX_VALUE); case I32: return asInteger(o, Integer.MIN_VALUE, Integer.MAX_VALUE); case I64: return asLong(o); case DOUBLE: return asDouble(o); case ENUM: if (o instanceof PEnumValue) { PEnumValue verified = ((PEnumDescriptor) descriptor).findById(((PEnumValue) o).asInteger()); if (o.equals(verified)) { return verified; } } else if (o instanceof Number) { return ((PEnumDescriptor) descriptor).findById(((Number) o).intValue()); } else if (o instanceof Numeric) { return ((PEnumDescriptor) descriptor).findById(((Numeric) o).asInteger()); } else if (o instanceof CharSequence) { return ((PEnumDescriptor) descriptor).findByName(o.toString()); } throw new ProvidenceConfigException("Unable to cast " + o.getClass().getSimpleName() + " to enum " + descriptor.getQualifiedName()); case MESSAGE: if (o instanceof PMessage) { PMessage message = (PMessage) o; if (descriptor.equals(message.descriptor())) { return o; } throw new ProvidenceConfigException("Message type mismatch: " + message.descriptor().getQualifiedName() + " is not compatible with " + descriptor.getQualifiedName()); } else { throw new ProvidenceConfigException(o.getClass().getSimpleName() + " is not compatible with message " + descriptor.getQualifiedName()); } case STRING: return asString(o); case BINARY: if (o instanceof Binary) { return o; } else { throw new ProvidenceConfigException(o.getClass() .getSimpleName() + " is not compatible with binary"); } case LIST: { PList<Object> list = (PList) descriptor; return list.builder() .addAll(asCollection(o, list.itemDescriptor())) .build(); } case SET: { PSet<Object> set = (PSet) descriptor; return set.builder() .addAll(asCollection(o, set.itemDescriptor())) .build(); } case MAP: { PMap<Object, Object> map = (PMap) descriptor; return map.builder() .putAll(asMap(o, map.keyDescriptor(), map.itemDescriptor())) .build(); } default: throw new IllegalStateException("Unhandled field type: " + descriptor.getType()); } } private  ProvidenceConfigUtil(); static Path canonicalFileLocation(@Nonnull Path file); static final String UNDEFINED; }
@Test public void testAsType() throws ProvidenceConfigException { assertThat(asType(PPrimitive.STRING, null), is(nullValue())); assertThat(asType(Value.kDescriptor, Value.SECOND), is(Value.SECOND)); assertThat(asType(Value.kDescriptor, 2), is(Value.SECOND)); assertThat(asType(Value.kDescriptor, "SECOND"), is(Value.SECOND)); assertThat(asType(Value.kDescriptor, (Numeric) () -> 2), is(Value.SECOND)); try { asType(Value.kDescriptor, Value.kDescriptor); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Unable to cast _Descriptor to enum config.Value")); } try { asType(Value.kDescriptor, PApplicationExceptionType.INVALID_MESSAGE_TYPE); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Unable to cast PApplicationExceptionType to enum config.Value")); } Service service = Service.builder().build(); Database db = Database.builder().build(); assertThat(asType(Service.kDescriptor, service), is(service)); try { asType(Service.kDescriptor, db); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Message type mismatch: config.Database is not compatible with config.Service")); } try { asType(Service.kDescriptor, "foo"); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("String is not compatible with message config.Service")); } assertThat(asType(PPrimitive.BINARY, Binary.fromHexString("abcd")), is(Binary.fromHexString("abcd"))); try { asType(PPrimitive.BINARY, 123); fail("no exception"); } catch (ProvidenceConfigException e) { assertThat(e.getMessage(), is("Integer is not compatible with binary")); } assertThat(asType(PList.provider(PPrimitive.STRING.provider()).descriptor(), ImmutableList.of(1, 2)), is(ImmutableList.of("1", "2"))); assertThat(new ArrayList((Collection) asType(PSet.sortedProvider(PPrimitive.STRING.provider()).descriptor(), ImmutableList.of(3, 4, 2, 1))), is(ImmutableList.of("1", "2", "3", "4"))); Map<String,String> map = (Map) asType(PMap.sortedProvider(PPrimitive.STRING.provider(), PPrimitive.STRING.provider()).descriptor(), ImmutableMap.of(1, 2, 3, 4)); assertThat(map, is(instanceOf(ImmutableSortedMap.class))); assertThat(map, is(ImmutableMap.of("1", "2", "3", "4"))); try { asType(PPrimitive.VOID, "true"); fail("no exception"); } catch (IllegalStateException e) { assertThat(e.getMessage(), is("Unhandled field type: void")); } }