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

EmbeddedType not supported? #16

Open
rwinzhang opened this issue Oct 10, 2018 · 3 comments
Open

EmbeddedType not supported? #16

rwinzhang opened this issue Oct 10, 2018 · 3 comments

Comments

@rwinzhang
Copy link

I have a struct like below

type Model struct {
    ID uuid.UUID
}

type User struct {
    Model
    Name string
}

and I am trying to do something like this

var UserFactory = factory.NewFactory(&models.User{}).
    Attr("ID", func(args factory.Args) (interface{}, error) {
        id := uuid.NewV4()
        return id, nil
    })

and I got this No such attribute name: ID, I don't have idea how to resolve this.

@ready4god2513
Copy link

Were you ever able to resolve this? It's an issue I'm dealing with as well. I'll keep searching for the answer and update if I find anything.

@ready4god2513
Copy link

What it looks like you can do is create a SubFactory. Something like this:

userFactory := factory.NewFactory(&models.User{}).
		Attr("User", func(_ factory.Args) (interface{}, error) {
			u := &hubc.User{
				FirstName: randomdata.FirstName(randomdata.RandomGender),
				LastName:  randomdata.LastName(),
			}

			name := strings.ToLower(u.FirstName) + randomdata.StringNumberExt(1, "", 3)
			domain := strings.ToLower(u.LastName) + ".com"
			u.Email = name + "@" + domain

			return *u, nil
		}).
		SubFactory("Meta", factory.NewFactory(models.Meta{}).
			Attr("UpdatedAt", func(_ factory.Args) (interface{}, error) {
				return time.Now(), nil
			}).
			Attr("CreatedAt", func(_ factory.Args) (interface{}, error) {
				return time.Now(), nil
			}).
			Attr("UpdatedBy", func(_ factory.Args) (interface{}, error) {
				return models.RobotID, nil
			}).
			Attr("CreatedBy", func(_ factory.Args) (interface{}, error) {
				return models.RobotID, nil
			}),
		).
		OnCreate(f.onCreate)

	return userFactory

@rwinzhang
Copy link
Author

@ready4god2513 I haven't resolved that either

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