Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to marshal/unmarshal custom types #76

Closed
patachi opened this issue Nov 5, 2018 · 1 comment
Closed

how to marshal/unmarshal custom types #76

patachi opened this issue Nov 5, 2018 · 1 comment

Comments

@patachi
Copy link

patachi commented Nov 5, 2018

Is there any interface out there to implement it for our types in order to achieve custom bindings ?

//"CREATE TABLE testKS.Foos (id uuid PRIMARY KEY, user_id text, bars map<text,int>)"
var tableName = "testKS.Foos"

type Foo struct {
	ID        gocql.UUID `db:"id"`
	UserID    string     `db:"user_id"`
	Bars []Bar      `db:"bars"`
}

type Bar struct {
	Key   string `db:"key"`
	Value int    `db:"value"`
}

func FetchFoo(session *gocql.Session) {
	qry, _ := qb.Select(tableName).ToCql()
	var items []*Foo
	err := gocqlx.Select(&items, session.Query(qry))
	if err != nil {
		log.Fatal(err) // can not marshal []Bar into map(varchar, int)
	}
	return items
}

func InsertFoo(session *gocql.Session, foo Foo) {
	qry, names := qb.Insert(tableName).
		Columns("id", "user_id", "bars").
		ToCql()
	err := gocqlx.Query(session.Query(qry), names).BindStruct(&foo).ExecRelease()
	if err != nil {
		log.Fatal(err) // can not unmarshal map(varchar, int) into *[]Bar
	}
}
@mmatczuk
Copy link
Contributor

Hi @patachi,
this is more of a question about gocql, I'd suggest adding type BarSlice []Bar and implementing gocql.Marshaller and gocql.Unmarshaller interfaces, see for example https://github.com/scylladb/gocqlx/blob/master/iterx_test.go#L27.

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

No branches or pull requests

2 participants