Resolving nested structure based on some field

Hello,

I am designing a notification system where each notification is a document structured like this:

{
  type: "like" | "reply" | "mention",
  receiver: Profile,
  time: number,
  subdata: {} // Structure depends on the value of "type"
}

When resolving those documents, I need to use nested if-then-else to resolve subdata:

If(
  Equals(Var("type"), "like"),
  Call("resolve_like_notif", Var("notif")),
  If(
    Equals(Var("type"), "reply"),
    Call("resolve_reply_notif", Var("notif")),
    If(
      Equals(Var("type"), "mention"),
      Call("resolve_mention_notif", Var("notif")),
      null
    )
  )
)

Is there a cleaner way to do this?

1 Like

How about something like this:

Let({
  type: 'like',
  notif: 'some var',
  cases: {
    like: "resolve_like_notif",
    reply: "resolve_reply_notif",
    mention: "resolve_mention_notif"
  },
  selectedFunction: Select(Var('type'), Var('cases'), null)
},
  If(
    IsNull(Var('selectedFunction')),
    Abort('Chosen function invalid'),
    Call(Var('selectedFunction'), Var('notif'))
  )
)
3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.