Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Question: Expected a map got 'struct' #38

Open
josebalius opened this issue Oct 8, 2015 · 11 comments
Open

Question: Expected a map got 'struct' #38

josebalius opened this issue Oct 8, 2015 · 11 comments
Assignees

Comments

@josebalius
Copy link

So after bypassing the issues talked about on #29 and #37 I stopped using a custom type (for now) and was able to insert the struct without a problem, yay!

Now, I am trying to get it out, keep getting this error:

panic: 2 error(s) decoding:

* 'columns[0]' expected a map, got 'struct'
* 'source' expected a map, got 'struct'

The struct has a field called "columns" that holds a slice of "column" structs, and "source" is a struct as well. My question is how can I get it to be okay with it being a struct? Or does this mean that I should convert the whole struct to a map before inserting so that I can then pull it out later and do whatever conversions I need?

What I'm I doing wrong? Tried looking at the mapstructure documentation but couldn't find much.

@dennwc
Copy link
Collaborator

dennwc commented Oct 9, 2015

Could you please provide the structure definition as well? Are these struct types exported?

@josebalius
Copy link
Author

@dennwc

Here is the type structure

type Source struct {
    Id   string `mapstructure:"id"`
    Type int    `mapstructure:"type"`
}

type DataSourceColumn struct {
    Name string `mapstructure:"name"`
    Type string `mapstructure:"type"`
}

type DataSource struct {
    Id          string             `mapstructure:"id"`
    Name        string             `mapstructure:"name"`
    Description string             `mapstructure:"description"`
    Tags        []string           `mapstructure:"tags"`
    Source      Source             `mapstructure:"source"`
    Columns     []DataSourceColumn `mapstructure:"columns"`
    Owner       string             `mapstructure:"owner"`
    Deleted     bool               `mapstructure:"deleted"`
}

Here is the struct I inserted using the new doc.From API:

DataSource{
            Id:          "3389e87e-4a5f-4766-aa56-56d3693331af",
            Name:        "Business",
            Description: "Business Data",
            Tags:        []string{"Business", "Data"},
            Source: Source{
                Id:   "business_tbl",
                Type: Public,
            },
            Columns: []DataSourceColumn{DataSourceColumn{
                Name: "name",
                Type: "string",
            }},
            Owner:   "",
            Deleted: false,
        }

Insert went fine, when doing a simple query like select * from DataSources I get the error.

@dennwc dennwc self-assigned this Oct 9, 2015
@dennwc
Copy link
Collaborator

dennwc commented Oct 9, 2015

Thanks! I just realized, there the problem is. I'll fix it soon.

@josebalius
Copy link
Author

@dennwc Awesome! Let me know if I can help in anyway.

@dennwc dennwc closed this as completed in 00565a4 Oct 9, 2015
@josebalius
Copy link
Author

@dennwc seems to work nicely :) again thank you.

@josebalius
Copy link
Author

@dennwc I have another case of this happening. Here is the model

type ItemA struct {
   Id string `mapstructure:"id"`
   Items []ItemB `mapstructure:"items"`
}

type ItemB struct {
   Parent string `mapstructure:"parent"`
   Value string `mapstructure:"value"`
}

Query:

select *, $items as items from ItemA let $items = (select value from ItemB where parent = $parent.$current.id)

If I run that in orient, i get the results and items is filled in with array of values from the ItemB class, but when I run this in Go, I get the error:

* 'items[0]' expected a map, got 'struct'
* 'items[1]' expected a map, got 'struct'

Question is, does this type of serialization only work if the slice of objects is inserted into the same class? Obviously I am using let so they are not in the same class.

@dennwc
Copy link
Collaborator

dennwc commented Jan 22, 2016

@josebalius Could you please post a serialized version of result for me, so I can use it in serialization test directly?

Here is the example:

var docs []orient.Document
err := results.All(&docs)
data, _ := docs[0].Content()
out := base64.StdEncoding.EncodeToString(data)

@dennwc dennwc reopened this Jan 22, 2016
@josebalius
Copy link
Author

@dennwc Running into some errors, here is the code and logs

    var docs []orient.Document
    err := q.All(&docs)

    if err != nil {
        panic(err)
    }

    log.Printf("%+v", docs[0])

    data, _ := docs[0].Content()
    out := base64.StdEncoding.EncodeToString(data)

    log.Println(out)

Logs:

16:49:54 app         | 2016/01/22 16:49:54 {BytesRecord:Bytes{RID: #0:0, Vers: 0, Data: [0]} serialized:false fieldsOrder:[] fields:map[] classname: dirty:false ser:<nil>}
16:49:54 app         | 2016/01/22 16:49:54 http: panic serving 127.0.0.1:49292: runtime error: invalid memory address or nil pointer dereference
goroutine 36 [running]:
net/http.(*conn).serve.func1(0xc8202ae000, 0x7c4940, 0xc82011c030)
    /usr/local/go/src/net/http/server.go:1287 +0xb5
gopkg.in/istreamdata/orientgo%!e(MISSING)v2.(*Document).Content(0xc8202c4100, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/josebalius/go/src/gopkg.in/istreamdata/orientgo.v2/document.go:96 +0x1c9
main.PlacesModel.GetPlaces(0xc820114090, 0x7c5d90, 0xc820114090, 0x7c5db8, 0xc82018c950, 0x7c5e00, 0xc82018c960, 0xc82011e390, 0x24, 0x0, ...)
    /Users/josebalius/go/src/app-service-places/model.go:730 +0x59a
main.getPlaces(0xb61220, 0xc8202ae0b0, 0xc8202b6000, 0x0, 0x0, 0xc820122320)

@dennwc
Copy link
Collaborator

dennwc commented Jan 22, 2016

Returned Document seems to be empty. Maybe there are some other documents in the set? In previous case the error was about some type conversion, so it definitely found "items" field at least for one result.

@josebalius
Copy link
Author

That's the problem, I know some results are being returned, if I run it in orient, its fine, this is the query with let btw, maybe q.all is having problems? I don't know :/

@dennwc
Copy link
Collaborator

dennwc commented Jan 22, 2016

"All" will not touch the result if the destination is document, or a slice of documents, so it's not the cause.
Maybe you should try to print all results from that slice. There is a chance, that one of document will be not nil.
What version of driver are you using, btw? orientgo.v2/document.go:96 is pointing to a wrong place in my code, so I can't even fix NPE, that you encountered.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants