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

Features request #27

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open

Features request #27

wants to merge 24 commits into from

Conversation

hyuti
Copy link

@hyuti hyuti commented Jan 17, 2023

Hi folks, I create this PR to solve some problems I face while i was working on other project which used this repository. By the way, my English is not quite good, so please forgive me if I make any mistakes.
Here are problems:

  1. Retrieves data from instance which is generated by subfactory:
    Let say we have following structs:
type Customer struct{
       ID int
}
type Order struct{
       CustomerId int
}

This is the most common relationship where a customer can have one or more orders. And I know in the most of cases we show relationship between them through the way we embed Customer into Order struct. However, there are some cases that we just want some fields to represent the relationship between entities such as the above example which Order entity only has ID of Customer.
So that would be great this repository give us some ways to work around this situation. So I defined a type called Formatter which is alias of func(interface{}) (interface{}, error)
For instance, if we only want ID field from Customer after SubFactory populated for us, we pass formatters as arguments to SubFactory function

var OrderFactory = factory.NewFactory(
	&Order{},

// define CustomerID before CustomerName so that CustomerID field is generated before CustomerName
// user a formatter to retrieve ID from Customer after Customer is created by CustomerFactory
).SubFactory("CustomerID", CustomerFactory, func(i interface{}) (interface{}, error) {
	e, ok := i.(*Customer)
	if !ok {
		return nil, fmt.Errorf("unexpected type %t", i)
	}
	return e.ID, nil
})
  1. generation ordering attribute cannot be controlled:
    Let say you have following struct:
type Order struct{
       CustomerName string
       CustomerId int
}

you need to ensure customer id field is generated through subfactory or whatever before customer name is generated, but surprisingly you mixed the order of these two fields or the above struct comes from another package which is not owned by you so that you cannot modify the order as you wish which means that the above struct should be as following to meet our requirements:

type Order struct{
      CustomerId int
      CustomerName string
}

Old implementations limit this usecase, we have to jumb back to the file where the struct lives and modify the order of the fields in the struct, or even in the worse case, the struct is not yours which is owned by someone else, so you cannot modify the struct as I mention, there is still a way for this case, you copy the struct. Although we still have ways to handle these cases, why don't we enhance the repository to meet our requirements conveniently.

See example in ordering-attr-with-formatter in the example folder for more detail on how usage.

Feel free to feedback!

Thanks!

@hyuti hyuti changed the title Add new features Features request Jan 23, 2023
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

Successfully merging this pull request may close these issues.

1 participant