Skip to content
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

#2772: PhContainingUTF8 to print strings correctly #2776

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion eo-runtime/src/main/java/EOorg/EOeolang/EOerror.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eolang.Attr;
import org.eolang.ExAbstract;
import org.eolang.ExFailure;
import org.eolang.PhContainingUTF8;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.Versionized;
Expand Down Expand Up @@ -115,7 +116,7 @@ public static final class ExError extends ExAbstract {
* @param enclosure Enclosure inside the error
*/
public ExError(final Phi enclosure) {
super(enclosure.toString());
super(EOerror.ExError.safeMessage(enclosure));
this.enc = enclosure;
}

Expand All @@ -126,5 +127,29 @@ public ExError(final Phi enclosure) {
public Phi enclosure() {
return this.enc;
}

/**
* Retrieve message from enclosure safely.
* @param enclosure Enclosure.
* @return String message.
* @checkstyle IllegalCatchCheck (20 lines)
*/
private static String safeMessage(final Phi enclosure) {
String result;
if (enclosure == null) {
result = "null Phi";
} else {
try {
result = new PhContainingUTF8(enclosure).toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@levBagryansky if this class is an instance of Phi and it makes phi objects safer by properly printing their toString(), why do we use only here, in this private method? Why don't we decorate all our phi objects with this decorator?

Copy link
Member Author

@levBagryansky levBagryansky Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 This class is correctly to use in order to print bytes of data as UTF8 String. For example, PhContainingUTF8(EOint) produces something strange. We can also use this class in Data.ToPhi(java.util.String)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@levBagryansky maybe we shouldn't call this class Phi... and should not make it a decorator of Phi? It doesn't look like a decorator to me, but a "smart" class: https://www.yegor256.com/2016/04/26/why-inputstream-design-is-wrong.html It just takes an existing instance of Phi, calls its method and then post-process the returned data. It doesn't become phi, it only is a client of Phi.

} catch (final Throwable first) {
try {
result = enclosure.toString();
} catch (final Throwable second) {
result = enclosure.getClass().toString();
}
}
}
return result;
}
}
}
89 changes: 89 additions & 0 deletions eo-runtime/src/main/java/org/eolang/PhContainingUTF8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang;

import java.nio.charset.StandardCharsets;

/**
* Class to print {@link Phi} objects with UTF8 String data correctly.
* @since 0.35
* @checkstyle AbbreviationAsWordInNameCheck (5 lines)
*/
public final class PhContainingUTF8 implements Phi {

/**
* Origin.
*/
private final Phi origin;

/**
* Ctor.
* @param origin Origin.
*/
public PhContainingUTF8(final Phi origin) {
this.origin = origin;
}

@Override
public String toString() {
return String.format(
"{%s: Δ as String = \"%s\"}",
this.origin.toString(),
new String(
new Dataized(this.origin).take(),
StandardCharsets.UTF_8
)
);
}

@Override
public Phi copy() {
return this.origin.copy();
}

@Override
public Attr attr(final int pos) {
return this.origin.attr(pos);
}

@Override
public Attr attr(final String name) {
return this.origin.attr(name);
}

@Override
public String locator() {
return this.origin.locator();
}

@Override
public String forma() {
return this.origin.forma();
}

@Override
public String φTerm() {
return this.origin.φTerm();
}
}
54 changes: 54 additions & 0 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOerrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@
*/
package EOorg.EOeolang;

import org.eolang.AtComposite;
import org.eolang.AtOnce;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.ExAbstract;
import org.eolang.PhCopy;
import org.eolang.PhDefault;
import org.eolang.PhWith;
import org.eolang.Phi;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -55,4 +62,51 @@ void makesToxicObject() {
);
}

@Test
void getsReadableError() {
ExAbstract error = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@levBagryansky can we make this variable final?
For example set it to null in try block and set to exc in catch block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxonfjvipon We cannot do it here, I get the error Variable 'error' might already have been assigned to: 73

try {
new Dataized(new MyError()).take();
} catch (final ExAbstract exc) {
error = exc;
}
assert error != null;
MatcherAssert.assertThat(
error.toString(),
Matchers.containsString("qwerty")
);
}

/**
* The object below.
* [] > my-error
* error > @
* "qwerty"
* @since 0.35
* @checkstyle JavadocStyleCheck
*/
private static final class MyError extends PhDefault {

/**
* Ctor.
*/
MyError() {
this.add(
"φ",
new AtOnce(
new AtComposite(
this,
rho -> new PhWith(
new PhCopy(
Phi.Φ.attr("org").get().attr("eolang").get().attr("error").get()
),
"α",
new Data.ToPhi("qwerty")
)
)
)
);
}
}

}