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 handle bson.ObjectID ? #162

Open
0xvoila opened this issue Oct 12, 2018 · 3 comments
Open

How to handle bson.ObjectID ? #162

0xvoila opened this issue Oct 12, 2018 · 3 comments

Comments

@0xvoila
Copy link

0xvoila commented Oct 12, 2018

Hello,

I have the following structure

type StudentModel struct {
	ID             bson.ObjectID               `bson:"_id,omitempty" jsonapi:"primary,students"`
	Name           StudentName          `bson:"name" jsonapi:"attr,name"`
	Email          string               `jsonapi:"attr,email"`
	Password       string               `bson:"password" jsonapi:"attr,password"`
	Phone          string               `bson:"phone" jsonapi:"attr,phone"`
	Country        string               `bson:"country" jsonapi:"attr,country"`
	Timezone       string               `bson:"name.timezone" jsonapi:"attr,timezone"`
	Org            string               `bson:"name.org" jsonapi:"attr,org"`
	Registration   []*RegistrationModel `jsonapi:"relation,registrations"`
	StudentProfile *StudentProfileModel `jsonapi:"relation,studentprofile"`
}

Jsonapi throws an error when doing

jsonapi.MarshalPayload(buff, student)

I think it can marshal only string and int.

If it is so then what is the right way to do this ?

@aren55555
Copy link
Contributor

I'm not sure where the type bson.ObjectID comes from as there is no import statement. Is it your own type, or a type supplied by another 3rd party Go package?

@3bb
Copy link

3bb commented Dec 15, 2018

@aren55555 bson.ObjectID is a specific ID for MongoDB documents.

@mwmahlberg
Copy link

I tracked this one down to

node.ID = v.Interface().(string)

I think it could be remedied with

case reflect.String:
	if s, ok := v.Interface().(fmt.Stringer); ok {
		node.ID = s.String()
	} else {
		node.ID = v.Interface().(string)
	}

The reason behind the proposed change would be that every fmt.Stringer could be used as a key.

Any objections?

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

4 participants