progrium / darwinkit

Native Mac APIs for Go. Previously known as MacDriver
MIT License
4.42k stars 147 forks source link

Fix issues with map type #242

Closed programmingkidx closed 6 months ago

programmingkidx commented 6 months ago

In the function type_convertion.go:convertToGoValue() it makes a call to reflect.ValueOf() for the reflect.Map case. This should not be done because the value that ToGoMap() returns is already of type reflect.Value. Also it makes type assertions fail for map types.

This program was used to test these two patches:

// Description: Test the (NS)Error class

package main

import f "github.com/progrium/macdriver/macos/foundation"
import "fmt"
import "github.com/progrium/macdriver/objc"
import "reflect"
import "github.com/progrium/macdriver/macos/appkit"

func main() {

    // declare the variables
    var domain f.ErrorDomain = "My Domain"
    code := -99
    var userInfo map[f.ErrorUserInfoKey]objc.IObject
    userInfo = make(map[f.ErrorUserInfoKey]objc.IObject)
    userInfo["one"] = f.String_StringWithString("ONE")
    userInfo["two"] = f.String_StringWithString("TWO")
    userInfo["three"] = f.String_StringWithString("THREE")
    userInfo["window"] = appkit.NewWindowWithSize(640, 480)

    // create the (NS)Error object
    myError := f.Error_ErrorWithDomainCodeUserInfo(domain, code, userInfo)

    // print out error and check field values
    fmt.Println("Testing Error class\n")
    fmt.Println("error.LocalizedDescription():", myError.LocalizedDescription())
    fmt.Print("error.Code(): ", myError.Code())
    if myError.Code() == code {
        fmt.Println("...correct")
    } else {
        fmt.Println("...wrong")
    }

    fmt.Print("error.Domain(): ", myError.Domain())
    if myError.Domain() == domain {
        fmt.Println("...correct")
    } else {
        fmt.Println("...wrong")
    }

    fmt.Println("error.UserInfo() value:", myError.UserInfo())
    fmt.Println("error.UserInfo() type:", reflect.TypeOf(myError.UserInfo()))
    fmt.Println("\texpected type: map[foundation.ErrorUserInfoKey]objc.IObject)")

    fmt.Println("\nReturned userinfo values:")
    for k,v := range myError.UserInfo() {
        fmt.Printf("key: %s\tvalue: %s\n", k, v.Description())
    }

    fmt.Println("\nExpected userinfo values:")
    for k,v := range myError.UserInfo() {
        fmt.Printf("key: %s\tvalue: %s\n", k, v.Description())
    }
}

Before these patches this program would panic at the call to myError.UserInfo(). With the patches applied this program works correctly.

progrium commented 6 months ago

Sorry for delay, but would it be possible to get a simplified version of this test program in as an actual test for the package?

programmingkidx commented 6 months ago

I made this patch to test my tests. To apply it "cd" to the root level of your local repo and run this command: "patch -p1 < NSError-tests.patch".

I had to comment out the Dictionary and Array tests because they cause the test system to fail. This prevents the NSError tests from running.

With all my pull requests applied all the tests run successfully.

Here is the output on my machine: go test ./macos/foundation -v === RUN TestFoundationValid --- PASS: TestFoundationValid (0.00s) === RUN TestFoundationError === RUN TestFoundationError/Empty_Map === RUN TestFoundationError/Map_with_items_in_it === RUN TestFoundationError/Check_domain_and_code_parameters --- PASS: TestFoundationError (0.00s) --- PASS: TestFoundationError/Empty_Map (0.00s) --- PASS: TestFoundationError/Map_with_items_in_it (0.00s) --- PASS: TestFoundationError/Check_domain_and_code_parameters (0.00s) PASS ok github.com/progrium/macdriver/macos/foundation 0.109s NSError-tests.patch

progrium commented 6 months ago

Why not make this patch part of this PR?

programmingkidx commented 6 months ago

Done.