0

For a project I have to query a Mongo database with a RESTful API in Go.

There is my problem, I get this structure from Mongo.

{
    "_id" : "ObjectID",
    "id" : 1234,
    "project" : {
        "id" : 1,
        "name" : "A project"
    },
    "tracker" : {
        "id" : 2,
        "name" : "A tracker"
    },
    "status" : {
        "id" : 3,
        "name" : "A status"
    },
    "priority" : {
        "id" : 4,
        "name" : "A priority"
    },
    "author" : {
        "id" : 5,
        "name" : "An author"
    },
    "assigned_to" : {
        "id" : 6,
        "name" : "Assigned to"
    },
    "category" : {
        "id" : 7,
        "name" : "A category"
    },
    "subject" : "A subject",
    "description" : "A description",
    "done_ratio" : 0,
    "spent_hours" : 0.0,
    "total_spent_hours" : 0.0,
    "custom_fields" : [ 
        {
            "id" : 1,
            "name" : "",
            "value" : ""
        }, 
        {
            "id" : 2,
            "name" : "",
            "multiple" : true,
            "value" : [ 
                ""
            ]
        }

    ],
    "created_on" : "2018-12-21T15:22:57.000Z",
    "updated_on" : "2018-12-26T10:58:35.000Z",
    "journals" : [ 
        {
            "id" : 1,
            "user" : {
                "id" : 1,
                "name" : ""
            },
            "notes" : "",
            "created_on" : "2018-12-21T15:32:51Z",
            "details" : [ 
                {
                    "property" : "",
                    "name" : "",
                    "new_value" : ""
                }
            ]
        }, 
        {
            "id" : 2,
            "user" : {
                "id" : 1,
                "name" : ""
            },
            "notes" : "",
            "created_on" : "2018-12-21T15:33:54Z",
            "details" : []
        }
    ]
}

As you can see, there is two elements called custom_fields and journals which are list of object. But sometimes it can be only a unique object not a list.

In my Go API, I created the following structure:

type Custom struct {
    Id int      `json:"id" bson:"id"`
    Name string `json:"name" bson:"name"`
    Multiple bool `json:"multiple" bson:"multiple"`
    Value []string `json:"value" bson:"value"`
}

type Journal struct {
    ID   int `json:"id" bson:"id"`
    User struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"user" bson:"user"`
    Note       string `json:"notes" bson:"notes"`
    Created_on string `json:"created_on" bson:"created_on"`
    Details    struct {
        Property  string `json:"property" bson:"property"`
        Name      string `json:"name" bson:"name"`
        Old_Value string `json:"old_value" bson:"old_value"`
        New_Value string `json:"new_value" bson:"new_value"`
    } `json:"details" bson:"details"`
}

type MainStruct struct {
    ID      int `json:"id" bson:"id"`
    Project struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"project" bson:"project"`
    Tracker struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"tracker" bson:"tracker"`
    Status struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"status" bson:"status"`
    Priority struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"priority" bson:"priority"`
    Author struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"author" bson:"author"`
    Assigned_to struct {
        ID   int    `json:"id" bson:"id"`
        Name string `json:"name" bson:"name"`
    } `json:"assigned_to" bson:"assigned_to"`
    Subject      string    `json:"subject" bson:"subject"`
    Description  string    `json:"description" bson:"description"`
    Due_date     time.Time `json:"due_date" bson:"due_date"`
    Done_ratio   int       `json:"done_ratio" bson:"done_ratio"`
    CustomFields []Custom  `json:"custom_fields" bson:"custom_fields"`
    Created_on   time.Time `json:"created_on" bson:"created_on"`
    Updated_on   time.Time `json:"updated_on" bson:"updated_on"`
    Journals     []Journal `json:"journals" bson:"journals"`
}

But when I query MongoDb and get a document which as one custom_field or one journal, I get the following errors:

cannot decode string into a slice

I don't know how to do to make the one element into a slice of 1.

3
  • 1
    "sometimes it can be only a unique object not a list" - data that is not well-structured tends to be very difficult to work with in Go. Can you fix the data creation/insertion issue so that the data is consistent? Commented Dec 28, 2018 at 16:49
  • As one of the way it could be using this method for setting stackoverflow.com/questions/30891301/… Commented Dec 28, 2018 at 16:56
  • According to mholt.github.io/json-to-go , and indeed the json itself, Journal.Details needs to be a []struct. Commented Dec 28, 2018 at 17:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.