Fix rule value json un/marshaling
This commit is contained in:
parent
0a517bcd23
commit
79eda44cf1
@ -1,10 +1,5 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Access int
|
||||
|
||||
const (
|
||||
@ -21,18 +16,7 @@ type Rule struct {
|
||||
}
|
||||
|
||||
func (a *Access) UnmarshalJSON(data []byte) error {
|
||||
var i interface{}
|
||||
err := json.Unmarshal(data, &i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s, ok := i.(string)
|
||||
if !ok {
|
||||
return errors.New("Type assertion .(string) failed.")
|
||||
}
|
||||
|
||||
switch s {
|
||||
switch string(data) {
|
||||
case "allow":
|
||||
*a = Allow
|
||||
case "deny":
|
||||
@ -42,3 +26,18 @@ func (a *Access) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a Access) MarshalJSON() ([]byte, error) {
|
||||
var str string
|
||||
|
||||
switch a {
|
||||
case Allow:
|
||||
str = "allow"
|
||||
case Deny:
|
||||
str = "deny"
|
||||
default:
|
||||
str = "inherit"
|
||||
}
|
||||
|
||||
return []byte(`"` + str + `"`), nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user