alexejk / go-xmlrpc

An XML-RPC Client for Go
https://alexejk.io/article/handling-xmlrpc-in-go/
MIT License
19 stars 7 forks source link

How to decode a struct of arbitrary fields? #77

Closed ahoward1024 closed 8 months ago

ahoward1024 commented 8 months ago

Hi, I'm struggling to decode an XMLRPC example where there are is a struct with a number of members whose names are arbitrary, so I can't put them into a tag.

Example XML ``` TESTING1 id1009470 pub_date2020-01-11 00:00:00 titleTITLE titleTITLE2 pub_date2020-01-11 00:00:00 id1009879 titleTitle3 pub_date2020-01-13 17:16:49 id1304451 TESTING2 id1329812 pub_date2020-01-11 00:00:00 titleNewTitle titleNextTitle pub_date2021-01-11 00:00:00 id1489372 titleTitle12 pub_date2020-01-13 17:16:49 id1229276 ```

The following structure can be successfully decoded into:

type DataType struct {
    Id      string `json:"id" xmlrpc:"id"`
    PubDate string `json:"pub_date" xmlrpc:"pub_date"`
    Title   string `json:"title" xmlrpc:"title"`
}

type Data struct {
    Struct struct {
        TESTING1 [][]DataType
        TESTING2 [][]DataType
    }
}

However, I cannot know the names where TESTING1 and TESTING2 are. They will change at runtime and the dataset will get added to and removed from, so those values are essentially arbitrary and therefore cannot be written directly into the struct. I also don't have control over the RPC API, so I cannot change the structure.

Any help would be greatly appreciated.

alexejk commented 8 months ago

Hi @ahoward1024 ,

Thanks for reaching out regarding this. I think it's a fairly new use-case I haven't heard of before. Typically when working with XML-RPC you have a knowledge of the struct field names, however in your case - as you said, you do not. What you need is a way to handle a struct as a map.

I took a stab at prototyping how this could be achieved and will likely open a PR fairly soon, however I'm still thinking about how to handle this in the best way from the compatibility perspective.

alexejk commented 8 months ago

@ahoward1024 I've pushed the PR for this fix. Could you please give it a try by pulling the dev branch into your go.mod?

You can do this by running go get -u alexejk.io/go-xmlrpc@decode-struct-as-map

It should give you a pseudo-version dependency entry, something like: alexejk.io/go-xmlrpc v0.4.2-0.20240214200317-abbf585c7d64

In order to solve your issue you should be able to now do something like this:

type Data struct {
    Map map[string][][]DataType
}

You can check out the added tests for more details.

ahoward1024 commented 8 months ago

@alexejk Apologies, I've been traveling and just got back to this.

I've pulled in your changes and they work flawlessly for our purpose. Thank you for getting back to me so quickly on this! :rocket:

I'll go ahead an close this out!

alexejk commented 8 months ago

Thanks for confirming, @ahoward1024 ! I've merged the PR and just triggered the 0.5.0 release. You can swap over to that version shortly (effectively just let it pull the latest version instead).