Skip to content

Calling prop.Properties.Set changes the type of value to a pointer #298

@jeremija

Description

@jeremija

I stumbled upon this problem while using this library in another project so I tried debugging it by adding the following test case to prop/prop_test.go and it fails:

func TestInt32(t *testing.T) {
	srv, err := dbus.SessionBus()
	if err != nil {
		t.Fatal(err)
	}
	defer srv.Close()

	cli, err := dbus.SessionBus()
	if err != nil {
		t.Fatal(err)
	}
	defer cli.Close()

	propsSpec := map[string]map[string]*Prop{
		"org.guelfey.DBus.Test": {
			"int32": {
				int32(100),
				true,
				EmitTrue,
				nil,
			},
		},
	}
	props := New(srv, "/org/guelfey/DBus/Test", propsSpec)

	obj := cli.Object(srv.Names()[0], "/org/guelfey/DBus/Test")

	comparePropValue(obj, "int32", int32(100), t)
	r := props.GetMust("org.guelfey.DBus.Test", "int32")
	if r != int32(100) {
		t.Errorf("expected r to be int32(100), but was %#v", r)
	}

	if err := props.Set("org.guelfey.DBus.Test", "int32", dbus.MakeVariant(int32(101))); err != nil {
		t.Fatalf("failed to set prop int32 to 101")
	}

	comparePropValue(obj, "int32", int32(101), t)
	r = props.GetMust("org.guelfey.DBus.Test", "int32")
	if r != int32(101) {
		t.Errorf("expected r to be int32(101), but was %#v", r)
	}
}
$ go test ./prop -run TestInt32
msg signal from org.freedesktop.DBus to :1.129 serial 2 path /org/freedesktop/DBus interface org.freedesktop.DBus member NameAcquired
  ":1.129"
deliver?
--- FAIL: TestInt32 (0.00s)
    prop_test.go:68: expected r to be int32(101), but was (*int32)(0xc000192120)
FAIL
FAIL    github.com/godbus/dbus/v5/prop  0.006s
FAIL

The first call to GetMust on the server side returns the int32(100), but after calling Set it starts returning a pointer.

This doesn't happen when I use SetMust, but I'd rather my code not to panic if the connection to DBus fails so I have to use SetMust, but then I have to check the type of value every time which is inconsistent and inconvenient:

	v, err := s.props.Get(dbusInterfaceName, "MyInt32Prop")
	if err != nil {
		return err
	}

	switch t := v.Value().(type) {
	case int32:
		value = t
	case *int32:
		value = *t
 	}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions