3
0

Fix rule value json un/marshaling

This commit is contained in:
Denis Arh 2019-02-22 21:19:07 +01:00
parent 0a517bcd23
commit 79eda44cf1

View File

@ -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
}