Skip to content

Commit e6ce668

Browse files
committed
Add custom marshallers for some ABAC types.
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
1 parent b32b37a commit e6ce668

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

client/types/abac.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package types
1010

1111
import (
12+
"encoding/json"
1213
"errors"
1314
"strings"
1415
"time"
@@ -133,6 +134,16 @@ type CapabilityState struct {
133134
Overrides []string
134135
}
135136

137+
func (cs CapabilityState) MarshalJSON() ([]byte, error) {
138+
return json.Marshal(&struct {
139+
Default DefaultAccessRule
140+
Overrides emptyStrings
141+
}{
142+
cs.Default,
143+
cs.Overrides,
144+
})
145+
}
146+
136147
// CapabilityDesc is an enhanced structure containing a capability value, its name, and a brief description
137148
type CapabilityDesc struct {
138149
Cap Capability
@@ -780,6 +791,16 @@ type TagAccess struct {
780791
Overrides []string //override sets an explicit allow or deny depending on Default state
781792
}
782793

794+
func (ta TagAccess) MarshalJSON() ([]byte, error) {
795+
return json.Marshal(&struct {
796+
Default DefaultAccessRule
797+
Overrides emptyStrings
798+
}{
799+
ta.Default,
800+
ta.Overrides,
801+
})
802+
}
803+
783804
// check returns two values:
784805
//
785806
// explicit: Whether or not the tag is in the TagAccess object.

0 commit comments

Comments
 (0)