Calling to_string() method on some data
Hi @sunng87
Thanks for your amazing work.
Suppose you have a data structure which is mapped to the following JSON:
{
"name": ".",
"type": "RRSIG",
"class": "IN",
"ttl": 172800,
"rd_length": 275,
"rdata": {
"type_covered": "DNSKEY",
"algorithm": "RSASHA256",
"labels": 0,
"ttl": 172800,
"sign_expiration": "20250110000000",
"sign_inception": "20241220000000",
"name": ".",
"signature": "blabla"
}
The rdata field is a Rust structure for which the to_string() exists. Is it possible to code a helper to call that to_string() method in a template ?
The rdata field is not always the same (actually an enum).
Thanks !
hello @dandyvica , handlebars uses serde_json's Value as internal type system because the original handlebarsjs is based on JavaScript. So it has no access to rdata's to_string when rendering a template. My idea is to implement a custom serde Serializer that serializes rdata as a string field and calls to_string there.
@sunng87 Thanks for your answer. I already have such a custom Serializer, but I don't know how to express it in the handlebars template file. Any hint ?
@dandyvica you can just try {{rdata}} and see output
It's weird because in some cases, the to_string() method seems to be called and result is right, other cases just prints out object:
contact.zonetransfer.me. TXT <[Remember to call or email Pippa on +44 123 4567890 or [email protected] when making DNS changes]>
dc-office.zonetransfer.me. A <143.228.181.132>
deadbeef.zonetransfer.me. AAAA <dead:beaf::>
dr.zonetransfer.me. LOC <[object]>
DZC.zonetransfer.me. TXT <[AbCdEfG]>
email.zonetransfer.me. NAPTR <[object]>
email.zonetransfer.me. A <74.125.206.26>
Hello.zonetransfer.me. TXT <[Hi to Josh and all his class]>
home.zonetransfer.me. A <127.0.0.1>
Info.zonetransfer.me. TXT <[ZoneTransfer.me service provided by Robin Wood - [email protected]. See http://digi.ninja/projects/zonetransferme.php for more information.]>
internal.zonetransfer.me. NS <intns1.zonetransfer.me.>
internal.zonetransfer.me. NS <intns2.zonetransfer.me.>
intns1.zonetransfer.me. A <81.4.108.41>
intns2.zonetransfer.me. A <167.88.42.94>
office.zonetransfer.me. A <4.23.39.254>
ipv6actnow.org.zonetransfer.me. AAAA <2001:67c:2e8:11::c100:1332>
owa.zonetransfer.me. A <207.46.197.32>
robinwood.zonetransfer.me. TXT <[Robin Wood]>
rp.zonetransfer.me. RP <[object]>
sip.zonetransfer.me. NAPTR <[object]>
sqli.zonetransfer.me. TXT <[' or 1=1 --]>
sshock.zonetransfer.me. TXT <[() { :]}; echo ShellShocked]>
staging.zonetransfer.me. CNAME <www.sydneyoperahouse.com.>
alltcpportsopen.firewall.test.zonetransfer.me. A <127.0.0.1>
Original template is:
{{#each messages}}
{{#each response.answer}}
{{name}} {{type}} <{{rdata}}>
{{/each}}
{{/each}}
Do you have other variants of rdata field? When it renders [object] it means it's a json object.