Skip to content

Commit

Permalink
Add custom marshallers for some ABAC types.
Browse files Browse the repository at this point in the history
Javascript really does not like to get a null when it expects an empty array,
but that's what Go will send if your struct contains e.g. a []string and
you don't explicitly initialize the slice. These custom marshallers make sure
we send an empty array, making the frontend's life easier.

Go desperately needs some way to tell the JSON encoder that we want this
behavior, see:

golang/go#37711
golang/go#27589
golang/go#27813
  • Loading branch information
floren committed Apr 21, 2023
1 parent b32b37a commit e6ce668
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions client/types/abac.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package types

import (
"encoding/json"
"errors"
"strings"
"time"
Expand Down Expand Up @@ -133,6 +134,16 @@ type CapabilityState struct {
Overrides []string
}

func (cs CapabilityState) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Default DefaultAccessRule
Overrides emptyStrings
}{
cs.Default,
cs.Overrides,
})
}

// CapabilityDesc is an enhanced structure containing a capability value, its name, and a brief description
type CapabilityDesc struct {
Cap Capability
Expand Down Expand Up @@ -780,6 +791,16 @@ type TagAccess struct {
Overrides []string //override sets an explicit allow or deny depending on Default state
}

func (ta TagAccess) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Default DefaultAccessRule
Overrides emptyStrings
}{
ta.Default,
ta.Overrides,
})
}

// check returns two values:
//
// explicit: Whether or not the tag is in the TagAccess object.
Expand Down

0 comments on commit e6ce668

Please sign in to comment.