Skip to content

Different treatment of value in a map vs in a struct #248

@or-else

Description

@or-else

Here is the minimum program which demonstrates the bug:

package main

import (
    rdb "github.com/dancannon/gorethink"
    "log"
)

type Custom int

func (Custom) MarshalJSON() ([]byte, error) {
    return []byte(`"MarshalJSON -- BUG HERE!1!"`), nil
}

type Nested struct {
    Test CustomStruct
}

type CustomStruct struct {
    InStruct Custom
}

func main() {
    session, err := rdb.Connect(rdb.ConnectOpts{Address: "localhost:28015"})
    if err != nil {
        log.Fatalln(err.Error())
    }
    rdb.DBDrop("test_db").RunWrite(session)
    rdb.DBCreate("test_db").RunWrite(session)
    rdb.DB("test_db").TableCreate("test").RunWrite(session)

    var val Custom = 123
    rdb.DB("test_db").Table("test").Insert(&Nested{
        Test: CustomStruct{InStruct: val}}).RunWrite(session)
    rdb.DB("test_db").Table("test").Insert(
        map[string]interface{}{"Test": map[string]interface{}{"InMap": val}}).RunWrite(session)
}

The output:

{
"Test": {
"InStruct": 123
} ,
"id":  "f0f77db2-9124-45ce-9c08-f5cbc1e82d78"
} {
"Test": {
"InMap":  "MarshalJSON -- BUG HERE!1!"
} ,
"id":  "f3b10b8f-ded9-41b2-bce7-f762ef250493"
}

This time I got the latest version of gorethink, so the bug is real :)
The basic difference from the example I gave earlier is the inner map declaration:
map[string]interface{} vs map[string]Custom

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions