kolo / xmlrpc

Implementation of XMLRPC protocol in Go language.
MIT License
158 stars 92 forks source link

throwing an error on ignored fields #81

Closed isaacwein closed 1 year ago

isaacwein commented 2 years ago

the xmlrpc encoder is throwing an error on ignored fields

xmlrpc_test.go:23: err: xmlrpc encode error: unsupported type


import (
    "context"
    "github.com/kolo/xmlrpc"
    "testing"
)

type Req struct {
    Id        int             `xmlrpc:"id"`
    Name      string          `xmlrpc:"name"`
    CTX       context.Context `xmlrpc:"-"` // this field is making the error
    OtherData struct{}        `xmlrpc:"-"`
}

func TestName(t *testing.T) {
    call, err := xmlrpc.EncodeMethodCall("test", &Req{
        Id:   1,
        Name: "test",
        CTX:  context.Background(),
    })
    if err != nil {
        t.Fatalf("err: %s", err.Error())
    }
    t.Logf("xml-rpc call: %s", call)
}

playground

icholy commented 2 years ago

This library doesn't support ignoring fields with - like encoding/json does. Adding support would technically be a backwards incompatible change, but I'd be surprised if anyone was actually affected. @kolo thoughts?

isaacwein commented 2 years ago

Why would it be a backward incomparable how can you usexmlrpc”-“ at all? Or may you can add a new suffix like xmlrpc=“,ignore”

On Sun, Sep 18, 2022 at 7:39 PM Ilia Choly @.***> wrote:

This library doesn't support ignoring fields with - like encoding/json does. Adding support would technically be a backwards incompatible change, but I'd be surprised if anyone was actually affected. @kolo https://github.com/kolo thoughts?

— Reply to this email directly, view it on GitHub https://github.com/kolo/xmlrpc/issues/81#issuecomment-1250411999, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIIO4JDNX65MQD7H7C6SNVDV66RZ3ANCNFSM6AAAAAAQPTS6CE . You are receiving this because you authored the thread.Message ID: @.***>

icholy commented 2 years ago

Currently, if you define a struct with a - field tag, it will use - as the field name in the generated xml:

For example:

type S struct {
    Name string `xmlrpc:"-"`
}

Will encode like this:

<value><struct><member><name>-</name><value><string>test</string></value></member></struct></value>

So adding support for - can potentially change the behaviour of existing code. In practice, I think that's very unlikely. I'm leaning towards simply adding support for -.

isaacwein commented 2 years ago

Ok, thanks for the clarification.

As of the JSON standard

As a special case, if the field tag is "-", the field is always omitted. Note that a field with name "-" can still be generated using the tag "-,".

go doc https://pkg.go.dev/encoding/json#:~:text=as%20a%20special%20case%2C%20if%20the%20field%20tag%20is%20%22%2D%22%2C%20the%20field%20is%20always%20omitted.%20note%20that%20a%20field%20with%20name%20%22%2D%22%20can%20still%20be%20generated%20using%20the%20tag%20%22%2D%2C%22

isaacwein commented 1 year ago

i think if a tag isxmlrpc:"-," the key should be <name>-</name> like json, not <name>-,</name> comma is a special character it is used for additional info about the tag xmlrpc:"key,omitempty"

https://goplay.tools/snippet/aalpRUBlTbJ

isaacwein commented 1 year ago

thanks alot for adding support for this it helped a lot