-
Notifications
You must be signed in to change notification settings - Fork 28
Improve TagDeserializer logic #224
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
Merged
tcheeric
merged 2 commits into
develop
from
codex/refactor-tagdeserializer-to-use-tag_decoders
Jul 30, 2025
Merged
Changes from all commits
Commits
Show all changes
2 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
This file contains hidden or 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
71 changes: 71 additions & 0 deletions
71
nostr-java-event/src/test/java/nostr/event/unit/TagDeserializerTest.java
This file contains hidden or 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,71 @@ | ||
| package nostr.event.unit; | ||
|
|
||
| import nostr.event.BaseTag; | ||
| import nostr.event.tag.AddressTag; | ||
| import nostr.event.tag.EventTag; | ||
| import nostr.event.tag.PriceTag; | ||
| import nostr.event.tag.UrlTag; | ||
| import nostr.event.tag.GenericTag; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| import static nostr.base.IEvent.MAPPER_AFTERBURNER; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class TagDeserializerTest { | ||
|
|
||
| @Test | ||
| void testAddressTagDeserialization() throws Exception { | ||
| String pubKey = "bbbd79f81439ff794cf5ac5f7bff9121e257f399829e472c7a14d3e86fe76984"; | ||
| String json = "[\"a\",\"1:" + pubKey + ":test\",\"ws://localhost:8080\"]"; | ||
| BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class); | ||
| assertInstanceOf(AddressTag.class, tag); | ||
| AddressTag aTag = (AddressTag) tag; | ||
| assertEquals(1, aTag.getKind()); | ||
| assertEquals(pubKey, aTag.getPublicKey().toString()); | ||
| assertEquals("test", aTag.getIdentifierTag().getUuid()); | ||
| assertEquals("ws://localhost:8080", aTag.getRelay().getUri()); | ||
| } | ||
|
|
||
| @Test | ||
| void testEventTagDeserialization() throws Exception { | ||
| String id = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346"; | ||
| String json = "[\"e\",\"" + id + "\",\"wss://relay.example.com\",\"root\"]"; | ||
| BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class); | ||
| assertInstanceOf(EventTag.class, tag); | ||
| EventTag eTag = (EventTag) tag; | ||
| assertEquals(id, eTag.getIdEvent()); | ||
| assertEquals("wss://relay.example.com", eTag.getRecommendedRelayUrl()); | ||
| assertEquals("root", eTag.getMarker().getValue()); | ||
| } | ||
|
|
||
| @Test | ||
| void testPriceTagDeserialization() throws Exception { | ||
| String json = "[\"price\",\"10.99\",\"USD\"]"; | ||
| BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class); | ||
| assertInstanceOf(PriceTag.class, tag); | ||
| PriceTag pTag = (PriceTag) tag; | ||
| assertEquals(new BigDecimal("10.99"), pTag.getNumber()); | ||
| assertEquals("USD", pTag.getCurrency()); | ||
| } | ||
|
|
||
| @Test | ||
| void testUrlTagDeserialization() throws Exception { | ||
| String json = "[\"u\",\"http://example.com\"]"; | ||
| BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class); | ||
| assertInstanceOf(UrlTag.class, tag); | ||
| UrlTag uTag = (UrlTag) tag; | ||
| assertEquals("http://example.com", uTag.getUrl()); | ||
| } | ||
|
|
||
| @Test | ||
| void testGenericFallback() throws Exception { | ||
| String json = "[\"unknown\",\"value\"]"; | ||
| BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class); | ||
| assertInstanceOf(GenericTag.class, tag); | ||
| GenericTag gTag = (GenericTag) tag; | ||
| assertEquals("unknown", gTag.getCode()); | ||
| assertEquals("value", gTag.getAttributes().get(0).getValue()); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.