0

Is there a was to access the []byte field of the Couchbase implementation in goLang or define a generic struct?

Use-case Not knowing the returned structure type at query time.

->Following code is not fully complete but should show what i'm doing<-

//two struct both share Id field
type Person struct {
    Id string `json:"id"`
    FirstName string `json:"firstName"`
}
type Building struct {
    Id string `json:"id"`
    Size int `json:"size"`
}
//sudo code for Couchbase connection, showing use of Default Connection
//edit 
import "github.com/couchbase/gocb/v2"
var dbCollection = cluster.Bucket(bucketName).DefaultCollection()

//show example get path variable from url eg /getFromDB/{id}
var pathVariables = mux.Vars(req)
id := pathVariables["id"]

//get request to db, and assign result to getResult, ignore error _
getResult, _ := dbCollection.Get(id, &gocb.GetOptions{})

//try and
dbPerson := Person{} //type is set to Person
getResult.Content(&dbPerson);

//If you print out the result, you see the content field as []bytes
// convert to string as **base 10 utf8** and you'll see the content; (minus a { at the start BUG?)
fmt.Printf("GetResult is: %+v\n",  *getResult)

//tried to get the result as bytes, to get the whole thing a map the content field later, but result is [123 125] or {
byteResult, errM := json.Marshal(*getResult)

//OR like, gives same output result [123 125] or { in base 10 utf8
var rawJson json.RawMessage
var errM error
rawJson, errM = json.Marshal(*getResult)

json.Unmarshal(rawBytes, &someStruct)

ASH
  • 980
  • 1
  • 9
  • 22
  • I"m not familiar with the package you're using (well, I'm only guessing, since you didn't specify which package it is). Did you try putting the content into a `[]byte` or `json.RawMessage`? – JimB Nov 11 '20 at 18:05
  • @JimB sorry, I"m using "github.com/couchbase/gocb/v2" and "encoding/json" for the encoding/Marshaling parts. I tried explicitly setting byteRestut too []byte and/or json.rawString, but i get the same. – ASH Nov 11 '20 at 19:41
  • I mean have `getResult.Content` decode into a `json.RawString`. I don't understand what you're trying to do by marshaling the `getResult`, since the content is not exported, plus the concept of "marshaling" would encode it as json when you're asking for the raw bytes. If you don't know the type, why not just use the json defaults in the first place? – JimB Nov 11 '20 at 20:08
  • I I'm not sure how i would decode as json.RawString, there is no RawString on json version i'm using. However I now think getting the []byte will not help me determine the type of the data returned, but i may prevent a second+ request to the database. I.e call getResult.Content(&person) returns {Id:1, FirstName ""} not full object so try different struct getResult.Content(&building) return {Id:2, size 100} – ASH Nov 11 '20 at 20:50
  • Sorry, I meant `RawMessage`, but copied your comment. If you don't ignore the error, you can see if the decoding was successful or not. If you don't know the type, just use the default types like you would with `encoding/json`. – JimB Nov 11 '20 at 20:56
  • ah ok. The default type, as in interface{} ? can you post as small code snippet, or doc link ? – ASH Nov 11 '20 at 21:10
  • https://pkg.go.dev/encoding/json#Unmarshal lists the default types – JimB Nov 11 '20 at 21:17

0 Answers0