@Override
protected Decimal64 deserializeImpl(final String product) {
- // FIXME: run value validation
- return Decimal64.valueOf(product);
+ final var typeDef = getTypeDefinition().orElseThrow();
+ final var parsed = Decimal64.valueOf(product);
+ final Decimal64 value;
+ try {
+ value = parsed.scaleTo(typeDef.getFractionDigits());
+ } catch (ArithmeticException e) {
+ throw new IllegalArgumentException("Value '" + product + "' does not match required fraction-digits", e);
+ }
+
+ // FIXME: check ranges
+ return value;
}
@Override
--- /dev/null
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.impl.codec;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class YT1437Test {
+ @Test
+ public void testDecimalFractionDigits() {
+ final var module = YangParserTestUtils.parseYangResource("/yt1437.yang").findModule("yt1437").orElseThrow();
+ final var foo = module.findDataChildByName(QName.create(module.getQNameModule(), "foo")).orElseThrow();
+ assertThat(foo, instanceOf(LeafSchemaNode.class));
+
+ final TypeDefinitionAwareCodec<?, ?> codec = TypeDefinitionAwareCodec.from(((LeafSchemaNode) foo).getType());
+ assertThat(codec, instanceOf(DecimalStringCodec.class));
+ final var cast = (DecimalStringCodec) codec;
+
+ final var one = cast.deserialize("20.0");
+ assertEquals(2, one.scale());
+ assertEquals("20.0", cast.serialize(one));
+
+ final var two = cast.deserialize("20.00");
+ assertEquals(2, two.scale());
+ assertEquals("20.0", cast.serialize(two));
+
+ final var three = cast.deserialize("20.000");
+ assertEquals(2, three.scale());
+ assertEquals("20.0", cast.serialize(three));
+ }
+}