samedi 2 mai 2020

Unmarshall only approved fields

I'm trying to give permissions to users to edit certain fields of a struct. This fields will vary depending on the action, the context, and the role the current user has.

At the moment I'm doing this in an imperative way but it's rather tedious and not scalable.

I thought having a list of approved_fields could be a nice and more scalable solution but I have no idea how to go through with this. I think reflection is the way to go but I don't know enough yet.

Any inspiration would help.

type Foo struct {
  Bar1 int
  Bar2 int
  Bar3 int
}

foo := Foo{}
approved_fields := []string{"Bar1", "Bar2"}
decode(json_data, &foo, approved_fields)

foo2 := Foo{}
approved_fields := []string{"Bar1", "Bar3"}
decode(json_data, &foo2, approved_fields)




Aucun commentaire:

Enregistrer un commentaire