-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Formatter for printing ReportNode #3317
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
commons/src/main/java/com/powsybl/commons/report/Formatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.commons.report; | ||
|
||
/** | ||
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>} | ||
*/ | ||
@FunctionalInterface | ||
public interface Formatter { | ||
Formatter DEFAULT = t -> t.getValue().toString(); | ||
|
||
String format(TypedValue value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
commons/src/test/java/com/powsybl/commons/report/FormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.commons.report; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Locale; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>} | ||
*/ | ||
class FormatterTest { | ||
|
||
@Test | ||
void test() { | ||
ReportNode root = ReportNode.newRootReportNode() | ||
.withMessageTemplate("formatTest", """ | ||
Formatter test message | ||
double default format: ${doubleDefaultFormat} | ||
double format based on type: ${doubleSpecificFormat} | ||
float format based on type: ${floatSpecificFormat} | ||
string default format: ${stringDefaultFormat} | ||
string format based on type: ${stringSpecificFormat}""") | ||
.withUntypedValue("doubleDefaultFormat", 4.35684975) | ||
.withTypedValue("doubleSpecificFormat", 4.4664798548, TypedValue.ACTIVE_POWER) | ||
.withTypedValue("floatSpecificFormat", 0.6f, TypedValue.IMPEDANCE) | ||
.withUntypedValue("stringDefaultFormat", "tiny") | ||
.withTypedValue("stringSpecificFormat", "This is a sentence which needs to be truncated", "LONG_SENTENCE") | ||
.build(); | ||
Formatter customFormatter = typedValue -> { | ||
if (typedValue.getType().equals(TypedValue.ACTIVE_POWER) && typedValue.getValue() instanceof Double d) { | ||
return String.format(Locale.CANADA_FRENCH, "%2.4f", d); | ||
} | ||
if (typedValue.getType().equals(TypedValue.IMPEDANCE) && typedValue.getValue() instanceof Float f) { | ||
return String.format(Locale.CANADA_FRENCH, "%.2f", f); | ||
} | ||
if (typedValue.getType().equals("LONG_SENTENCE") && typedValue.getValue() instanceof String s) { | ||
return s.substring(0, 18); | ||
} | ||
return typedValue.getValue().toString(); | ||
}; | ||
assertEquals(""" | ||
Formatter test message | ||
double default format: 4.35684975 | ||
double format based on type: 4,4665 | ||
float format based on type: 0,60 | ||
string default format: tiny | ||
string format based on type: This is a sentence""", root.getMessage(customFormatter)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can name it ReportFormatter or MessageFormatter to avoid confusion with technical / JDK formatting classes ?