Repository: gopcua/opcua
Pull request #157 (2019-03-28 07:04:27)

fix variant.Int()
The `Int()` method was not returning values for ints. Not sure if these methods are the right approach but this was broken.


Patch:
diff --git a/ua/variant.go b/ua/variant.go
index e6be9d5..c4695c4 100644
--- a/ua/variant.go
+++ b/ua/variant.go
@@ -311,6 +311,8 @@ func (m *Variant) Set(v interface{}) error {
 	return nil
 }
 
+// todo(fs): this should probably be StringValue or we need to handle all types
+// todo(fs): and recursion
 func (m *Variant) String() string {
 	switch m.Type() {
 	case TypeIDString:
@@ -320,7 +322,8 @@ func (m *Variant) String() string {
 	case TypeIDQualifiedName:
 		return m.Value.(*QualifiedName).Name
 	default:
-		return fmt.Sprintf("%v", m.Value)
+		return ""
+		//return fmt.Sprintf("%v", m.Value)
 	}
 }
 
@@ -348,11 +351,11 @@ func (m *Variant) Int() int64 {
 	switch m.Type() {
 	case TypeIDSByte:
 		return int64(m.Value.(int8))
-	case TypeIDUint16:
+	case TypeIDInt16:
 		return int64(m.Value.(int16))
-	case TypeIDUint32:
+	case TypeIDInt32:
 		return int64(m.Value.(int32))
-	case TypeIDUint64:
+	case TypeIDInt64:
 		return m.Value.(int64)
 	default:
 		return 0


Test patch:
diff --git a/ua/variant_test.go b/ua/variant_test.go
index 2b85ac7..5feb71a 100644
--- a/ua/variant_test.go
+++ b/ua/variant_test.go
@@ -309,3 +309,133 @@ func TestVariant(t *testing.T) {
 	}
 	RunCodecTest(t, cases)
 }
+
+func TestVariantUnsupportedType(t *testing.T) {
+	_, err := NewVariant(int(5))
+	if err == nil {
+		t.Fatal("got nil want err")
+	}
+}
+
+func TestVariantBool(t *testing.T) {
+	tests := []struct {
+		v interface{}
+		n bool
+	}{
+		{true, true},
+		{"", false},
+	}
+	for _, tt := range tests {
+		v, err := NewVariant(tt.v)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if got, want := v.Bool(), tt.n; got != want {
+			t.Fatalf("got %v want %v", got, want)
+		}
+	}
+}
+
+func TestVariantString(t *testing.T) {
+	tests := []struct {
+		v interface{}
+		n string
+	}{
+		{"a", "a"},
+		{&LocalizedText{Text: "a"}, "a"},
+		{&QualifiedName{Name: "a"}, "a"},
+		{int32(5), ""},
+	}
+	for _, tt := range tests {
+		v, err := NewVariant(tt.v)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if got, want := v.String(), tt.n; got != want {
+			t.Fatalf("got %v want %v", got, want)
+		}
+	}
+}
+
+func TestVariantFloat(t *testing.T) {
+	tests := []struct {
+		v interface{}
+		n float64
+	}{
+		{float32(5), 5},
+		{float64(5), 5},
+		{int32(5), 0},
+	}
+	for _, tt := range tests {
+		v, err := NewVariant(tt.v)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if got, want := v.Float(), tt.n; got != want {
+			t.Fatalf("got %v want %v", got, want)
+		}
+	}
+}
+
+func TestVariantInt(t *testing.T) {
+	tests := []struct {
+		v interface{}
+		n int64
+	}{
+		{int8(5), 5},
+		{int16(5), 5},
+		{int32(5), 5},
+		{int64(5), 5},
+		{"", 0},
+	}
+	for _, tt := range tests {
+		v, err := NewVariant(tt.v)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if got, want := v.Int(), tt.n; got != want {
+			t.Fatalf("got %v want %v", got, want)
+		}
+	}
+}
+
+func TestVariantUint(t *testing.T) {
+	tests := []struct {
+		v interface{}
+		n uint64
+	}{
+		{uint8(5), 5},
+		{uint16(5), 5},
+		{uint32(5), 5},
+		{uint64(5), 5},
+		{"", 0},
+	}
+	for _, tt := range tests {
+		v, err := NewVariant(tt.v)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if got, want := v.Uint(), tt.n; got != want {
+			t.Fatalf("got %v want %v", got, want)
+		}
+	}
+}
+
+func TestVariantTime(t *testing.T) {
+	tests := []struct {
+		v interface{}
+		n time.Time
+	}{
+		{time.Date(2019, 1, 1, 12, 13, 14, 0, time.UTC), time.Date(2019, 1, 1, 12, 13, 14, 0, time.UTC)},
+		{"", time.Time{}},
+	}
+	for _, tt := range tests {
+		v, err := NewVariant(tt.v)
+		if err != nil {
+			t.Fatal(err)
+		}
+		if got, want := v.Time(), tt.n; got != want {
+			t.Fatalf("got %v want %v", got, want)
+		}
+	}
+}