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

Allow option for nested values on slice fields #29

Open
damif94 opened this issue Oct 17, 2023 · 0 comments
Open

Allow option for nested values on slice fields #29

damif94 opened this issue Oct 17, 2023 · 0 comments

Comments

@damif94
Copy link

damif94 commented Oct 17, 2023

Intro

Hi!
For some time we have been using this library at my company, and the dot notation for nested field setting on subfactories has proven really useful. It make really the declaration of new instances with fixed data really elegant.

Now, I think a similar strategy could be carried over for slice nested fields.
If you like the idea, I could PR it myself.

Example

Context

For example (borrowed from our codebase):

type Player struct {
  ID          string
  FirstName   string
  LastName    string
  ExternalIds []ExternalID
}

type ExternalID struct {
  ID       string
  Provider string
}

our factories look like:

func playerFactoryBuilder(model any) *factory.Factory {
  return factory.NewFactory(
    model,
  ).Attr(
    "ID",
      AlphanumericGenerator(10),
  ).Attr(
    "FirstName", FirstNameGenerator,
  ).Attr(
    "LastName", LastNameGenerator,
  ).SubSliceFactory(
    "ExternalIds",
    externalIDFactory,
    func() int { return 2 },
  )
}

var externalIDFactory = NewFactory(
  models.ExternalID{},
).Attr(
  "Provider",
  ConstantGenerator("somewhat"),
).Attr(
  "ID",
  AlphanumericGenerator(10),
)

Proposal:

My proposal would work like this:

Scenario 1: Dot notation for nested array fields

func main() {
  for i := 0; i < 3; i++ {
    player := PlayerFactory.MustCreateWithOption(map[string]any{"ExternalIds.Provider": "some-provider"}).(*Player)
    for i, _ := range player.externalIds {
      fmt.Println("ID:", player.ExternalIds.[i].ID, " Provider:", player.ExternalIds[i].Provider)
    }
  }
}

would affect all slice generated instances, printing something like

ID: 12ad23d4bn  Provider: some-provider
ID: 32ad23d4bn  Provider: some-provider

Scenario 2: Dot notation for nested array fields on specific index in slice subfactory bounds

func main() {
  for i := 0; i < 3; i++ {
    player := PlayerFactory.MustCreateWithOption(map[string]any{"ExternalIds.1.Provider": "some-provider"}).(*Player)
    for i, _ := range player.externalIds {
      fmt.Println("ID:", player.ExternalIds.[i].ID, " Provider:", player.ExternalIds[i].Provider)
    }
  }
}

would affect only the instance generated at the specified index, printing something like

ID: 12ad23d4bn  Provider: somewhat
ID: 32ad23d4bn  Provider: some-provider

Scenario 3: Dot notation for nested array fields on specific index out of slice subfactory bounds

func main() {
  for i := 0; i < 3; i++ {
    player := PlayerFactory.MustCreateWithOption(map[string]any{"ExternalIds.2.Provider": "some-provider"}).(*Player)
    for i, _ := range player.externalIds {
      fmt.Println("ID:", player.ExternalIds.[i].ID, " Provider:", player.ExternalIds[i].Provider)
    }
  }
}

would panic with an error.

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

1 participant