From 9fe91a158e30e4022d2027a55027e1e5ae97640b Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 31 Mar 2022 22:25:15 +0200 Subject: [PATCH] Fix union stringValue() with Decimal64 UnionTemplate still things decimal64 maps to java.math.BigInteger, resulting in string conversion which does not compile. Fix the template to recognize Decimal64 as a proper CanonicalValue. JIRA: MDSAL-738 Change-Id: I0f161b62887f4aea6e0c625d105e48a20c8dc048 Signed-off-by: Robert Varga --- .../java/api/generator/UnionTemplate.xtend | 9 ++- .../java/api/generator/Mdsal738Test.java | 58 +++++++++++++++++++ .../resources/compilation/mdsal738/foo.yang | 17 ++++++ 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal738Test.java create mode 100644 binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal738/foo.yang diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend index cf6632036c..c940dea195 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend @@ -118,13 +118,12 @@ class UnionTemplate extends ClassTemplate { «ELSEIF BINARY_TYPE.equals(propRet)» ««« type binary return new «STRING.importedName»(«field»); - «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration - || propRet.fullyQualifiedName.startsWith("java.math")» - ««« type int*, decimal64 or enumeration* + «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration» + ««« type int* or enumeration* return «field».toString(); «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName) - && propRet.name.startsWith("Uint")» - ««« type uint* + && (propRet.name.startsWith("Uint") || "Decimal64".equals(propRet.name))» + ««« type uint*, decimal64 return «field».toCanonicalString(); «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType» ««« union type diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal738Test.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal738Test.java new file mode 100644 index 0000000000..8ebe52811a --- /dev/null +++ b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/Mdsal738Test.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022 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.mdsal.binding.java.api.generator; + +import static org.junit.Assert.assertNotNull; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class Mdsal738Test extends BaseCompilationTest { + private File sourcesOutputDir; + private File compiledOutputDir; + + @Before + public void before() throws IOException, URISyntaxException { + sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal738"); + compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal738"); + } + + @After + public void after() { + CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir); + } + + @Test + public void testUnionOfDecimal64() throws IOException, URISyntaxException { + generateTestSources("/compilation/mdsal738", sourcesOutputDir); + final var pmDataType = FileSearchUtil.getFiles(sourcesOutputDir).get("PmDataType.java"); + assertNotNull(pmDataType); + + final var content = Files.readString(pmDataType.toPath()); + FileSearchUtil.assertFileContainsConsecutiveLines(pmDataType, content, + " public String stringValue() {", + " if (_uint64 != null) {", + " return _uint64.toCanonicalString();", + " }", + " if (_int64 != null) {", + " return _int64.toString();", + " }", + " if (_decimal64 != null) {", + " return _decimal64.toCanonicalString();", + " }", + " throw new IllegalStateException(\"No value assigned\");", + " }"); + + CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir); + } +} diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal738/foo.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal738/foo.yang new file mode 100644 index 0000000000..fd97d7e555 --- /dev/null +++ b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/mdsal738/foo.yang @@ -0,0 +1,17 @@ +module foo { + namespace foo; + prefix foo; + + typedef pm-data-type { + type union { + type uint64; + type int64; + type decimal64 { + fraction-digits 2; + } + type decimal64 { + fraction-digits 17; + } + } + } +} -- 2.36.6