2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
// This file contains types and helper methods for wildcard containers.
|
|
|
|
// Wildcard container can contain either a finite set of elements or
|
|
|
|
// every possible element, in which case it is named `wildcard`.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WildStrings represents string set which can be wildcard.
|
|
|
|
type WildStrings struct {
|
|
|
|
Value []string
|
|
|
|
}
|
|
|
|
|
2021-05-04 11:48:50 +00:00
|
|
|
// WildPermissionDescs represents PermissionDescriptor set which can be wildcard.
|
|
|
|
type WildPermissionDescs struct {
|
|
|
|
Value []PermissionDesc
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Contains checks if v is in the container.
|
|
|
|
func (c *WildStrings) Contains(v string) bool {
|
|
|
|
if c.IsWildcard() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, s := range c.Value {
|
|
|
|
if v == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains checks if v is in the container.
|
2021-05-04 11:48:50 +00:00
|
|
|
func (c *WildPermissionDescs) Contains(v PermissionDesc) bool {
|
2020-03-18 15:21:12 +00:00
|
|
|
if c.IsWildcard() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, u := range c.Value {
|
|
|
|
if u.Equals(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsWildcard returns true iff container is wildcard.
|
|
|
|
func (c *WildStrings) IsWildcard() bool { return c.Value == nil }
|
|
|
|
|
|
|
|
// IsWildcard returns true iff container is wildcard.
|
2021-05-04 11:48:50 +00:00
|
|
|
func (c *WildPermissionDescs) IsWildcard() bool { return c.Value == nil }
|
2020-03-18 15:21:12 +00:00
|
|
|
|
|
|
|
// Restrict transforms container into an empty one.
|
|
|
|
func (c *WildStrings) Restrict() { c.Value = []string{} }
|
|
|
|
|
|
|
|
// Restrict transforms container into an empty one.
|
2021-05-04 11:48:50 +00:00
|
|
|
func (c *WildPermissionDescs) Restrict() { c.Value = []PermissionDesc{} }
|
2020-03-18 15:21:12 +00:00
|
|
|
|
|
|
|
// Add adds v to the container.
|
|
|
|
func (c *WildStrings) Add(v string) { c.Value = append(c.Value, v) }
|
|
|
|
|
|
|
|
// Add adds v to the container.
|
2021-05-04 11:48:50 +00:00
|
|
|
func (c *WildPermissionDescs) Add(v PermissionDesc) { c.Value = append(c.Value, v) }
|
2020-03-18 15:21:12 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
2020-11-13 18:46:26 +00:00
|
|
|
func (c WildStrings) MarshalJSON() ([]byte, error) {
|
2020-03-18 15:21:12 +00:00
|
|
|
if c.IsWildcard() {
|
|
|
|
return []byte(`"*"`), nil
|
|
|
|
}
|
|
|
|
return json.Marshal(c.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
2021-05-04 11:48:50 +00:00
|
|
|
func (c WildPermissionDescs) MarshalJSON() ([]byte, error) {
|
2020-03-18 15:21:12 +00:00
|
|
|
if c.IsWildcard() {
|
|
|
|
return []byte(`"*"`), nil
|
|
|
|
}
|
|
|
|
return json.Marshal(c.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
|
|
|
func (c *WildStrings) UnmarshalJSON(data []byte) error {
|
|
|
|
if !bytes.Equal(data, []byte(`"*"`)) {
|
|
|
|
ss := []string{}
|
|
|
|
if err := json.Unmarshal(data, &ss); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Value = ss
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
2021-05-04 11:48:50 +00:00
|
|
|
func (c *WildPermissionDescs) UnmarshalJSON(data []byte) error {
|
2020-03-18 15:21:12 +00:00
|
|
|
if !bytes.Equal(data, []byte(`"*"`)) {
|
2021-05-04 11:48:50 +00:00
|
|
|
us := []PermissionDesc{}
|
2020-03-18 15:21:12 +00:00
|
|
|
if err := json.Unmarshal(data, &us); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Value = us
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|