redundent / kotlin-xml-builder

A lightweight type safe builder to build xml documents in Kotlin
Apache License 2.0
155 stars 19 forks source link

Typesafe namespaces #49

Closed asm0dey closed 2 years ago

asm0dey commented 2 years ago

Currently one has to type local name fully. Indeed this is error-prone: one typo and your XML is invalid because you used non-existent namespace. Is it possible to add return type to the namespace method, which will be possible to reuse in attribute and element declaration? It son't break any existing API (seemingly) but will allow more type-safe manner of work with namespaces

redundent commented 2 years ago

Hey @asm0dey, Were you thinking something as simple as the follow?

xml("root") {
  val myNamespace = namespace("myns", "https://my.schema")

  "element"(myNamespace) {
    // ...
  }
}

resulting in

<root xmlns:myns="https://my.schema">
  <myns:element/>
</root>
asm0dey commented 2 years ago

Actually I like this particular syntax! Also we definitely need the same syntax for attributes. And also the root itself can be in some namespace, that's why I would prefer to have namespace declared even outside the root

вт, 5 июл. 2022 г., 00:13 Jason Blackwell @.***>:

Hey @asm0dey https://github.com/asm0dey, Were you thinking something as simple as the follow?

xml("root") { val myNamespace = namespace("myns", "https://my.schema")

"element"(myNamespace) { // ... } }

resulting in

— Reply to this email directly, view it on GitHub https://github.com/redundent/kotlin-xml-builder/issues/49#issuecomment-1174375178, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ4XAXIMDZQLMWCKJLWFPLVSNHYVANCNFSM52CYAJKQ . You are receiving this because you were mentioned.Message ID: @.***>

redundent commented 2 years ago

Yep, you're absolutely right. I'll try to work on getting this implemented tomorrow

redundent commented 2 years ago

@asm0dey I added this functionality in this PR https://github.com/redundent/kotlin-xml-builder/pull/50. I believe it should cover all the pieces you were looking for. If you want to have a look, feel free. I'll merge it and push the new version up later this afternoon (EST)

asm0dey commented 2 years ago

Hey, after some testing a hit the important issue. I thought that I can define default namespace just by creating namespace with empty name, but it leads to incorrect xml:

private val atom = Namespace("", "http://www.w3.org/2005/Atom")
xml("feed", "utf-8", XmlVersion.V10, atom) {
    "id"(atom) { -"root" }
    "title"(atom) { -"Asm0dey's books" }
    "updated"(atom) { -dtf.format(ZonedDateTime.now()) }
// and more

leads to following generated xml:

<?xml version="1.0" encoding="utf-8"?>
<:feed xmlns:="http://www.w3.org/2005/Atom">
        <:id>
                root
        </:id>
        <:title>
                Asm0dey&apos;s books
        </:title>
        <:updated>
                2022-07-08T23:31:58.120017514+03:00
        </:updated>
asm0dey commented 2 years ago

Also, API does not deduplicate the same code, with xmlns = atom.value explicitly specified I have following:

<?xml version="1.0" encoding="utf-8"?>
<:feed xmlns="http://www.w3.org/2005/Atom" xmlns:="http://www.w3.org/2005/Atom">
        <:id>
<!-- and so on -->
asm0dey commented 2 years ago

And more: if I just start using namespace on some element like this "title"(thread) { -"Asm0dey's books" } I would expect for the namespace thread to be declared at least on the element level, in best case at the root elent, but it isn't.

redundent commented 2 years ago

Originally, I intended for this library to be as simple/dumb as possible. However, after all the changes from the original code, that's not really possible anymore so I'm going to have to smarten it up a bit. I'll try to fix all these issues this weekend.

asm0dey commented 2 years ago

I actually think that the API can remain absolutely the same, it may become a bit more complex inside.

сб, 9 июл. 2022 г., 05:44 Jason Blackwell @.***>:

Originally, I intended for this library to be as simple/dumb as possible. However, after all the changes from the original code, that's not really possible anymore so I'm going to have to smarten it up a bit. I'll try to fix all these issues this weekend.

— Reply to this email directly, view it on GitHub https://github.com/redundent/kotlin-xml-builder/issues/49#issuecomment-1179464903, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ4XAWKBDYCZXUOLVAQAWDVTDRR5ANCNFSM52CYAJKQ . You are receiving this because you were mentioned.Message ID: @.***>

redundent commented 2 years ago

Version 1.8.0 has been released. This should include all the changes requested. Let me know if there are any issues with your usecase.