Groups metadata, how to use the code from the docs sample?

There is a sample in the documentation Heroic Labs Documentation | Groups

// Assuming a group metadata structure as follows
type GroupMetadata struct {
	Roles map[string][]string `json:"roles"`

metadata := &GroupMetadata {
  Roles: map[string][]string{
    "000d8152-3258-457b-905b-05a9223c5c8c": { "bouncer" },
    "2c0c8e80-fcbc-4b61-901a-dace129f45f5": { "bouncer", "vip" },

which shows that we can define a struct for a group metadata and then use it. But the sample doesn’t show how to write this struct to a group’s metadata. The problem is that the nk.GroupUpdate and nk.GroupCreate methods require map[string]interface{} parameter. So is the sample correct and if it is how can I write a custom struct to a group’s metadata?

Hello @Andrey, the example is incorrect, it should be:

metadata := map[string]any{
  "roles": map[string]any{
    "000d8152-3258-457b-905b-05a9223c5c8c": []string{"bouncer"},
    "2c0c8e80-fcbc-4b61-901a-dace129f45f5": []string{"bouncer", "vip"},

@gabriel kindly update the sample in the docs please :pray:

1 Like

Hello @sesposito, thanks!

It means that the code below (from the same sample) is incorrect too:

var metadata GroupMetadata
  if err := json.Unmarshal([]byte(groups[0].GetMetadata()), &metadata); err != nil {
    logger.Error("error deserializing metadata")
    return nil, runtime.NewError("error deserializing metadata", 13)

Thanks for pointing that out but I believe that the unmarshal code should work by just changing:

var metadata GroupMetadata


var metadata *GroupMetadata


metadata := &GroupMetadata{}
1 Like

Oh, yes, exactly, you are right :slight_smile:

Hi @Andrey, a fix for this has just been made to our documentation. Thanks.

1 Like