2020-03-18 15:21:12 +00:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
// This file contains types and helper methods for wildcard containers.
|
2022-04-20 18:30:09 +00:00
|
|
|
// A wildcard container can contain either a finite set of elements or
|
2020-03-18 15:21:12 +00:00
|
|
|
// every possible element, in which case it is named `wildcard`.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2024-08-24 19:03:41 +00:00
|
|
|
"slices"
|
2020-03-18 15:21:12 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// WildStrings represents a string set which can be a wildcard.
|
2020-03-18 15:21:12 +00:00
|
|
|
type WildStrings struct {
|
|
|
|
Value []string
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// WildPermissionDescs represents a PermissionDescriptor set which can be a wildcard.
|
2021-05-04 11:48:50 +00:00
|
|
|
type WildPermissionDescs struct {
|
2024-07-26 09:32:49 +00:00
|
|
|
Value []PermissionDesc
|
|
|
|
Wildcard bool
|
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
|
|
|
|
}
|
2024-08-24 19:03:41 +00:00
|
|
|
return slices.Contains(c.Value, v)
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2024-08-24 19:03:41 +00:00
|
|
|
return slices.ContainsFunc(c.Value, v.Equals)
|
2020-03-18 15:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// IsWildcard returns true iff the container is a wildcard.
|
2020-03-18 15:21:12 +00:00
|
|
|
func (c *WildStrings) IsWildcard() bool { return c.Value == nil }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// IsWildcard returns true iff the container is a wildcard.
|
2024-07-26 09:32:49 +00:00
|
|
|
func (c *WildPermissionDescs) IsWildcard() bool { return c.Wildcard }
|
2020-03-18 15:21:12 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Restrict transforms the container into an empty one.
|
2020-03-18 15:21:12 +00:00
|
|
|
func (c *WildStrings) Restrict() { c.Value = []string{} }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Restrict transforms the container into an empty one.
|
2024-07-26 09:32:49 +00:00
|
|
|
func (c *WildPermissionDescs) Restrict() {
|
|
|
|
c.Value = []PermissionDesc{}
|
|
|
|
c.Wildcard = false
|
|
|
|
}
|
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) }
|
|
|
|
|
2024-08-05 09:07:22 +00:00
|
|
|
// Add adds v to the container and converts container to non-wildcard (if it's still
|
|
|
|
// wildcard).
|
|
|
|
func (c *WildPermissionDescs) Add(v PermissionDesc) {
|
|
|
|
c.Value = append(c.Value, v)
|
|
|
|
c.Wildcard = false
|
|
|
|
}
|
2020-03-18 15:21:12 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the 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)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the 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)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-03-18 15:21:12 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2021-05-04 11:48:50 +00:00
|
|
|
func (c *WildPermissionDescs) UnmarshalJSON(data []byte) error {
|
2024-07-26 09:32:49 +00:00
|
|
|
c.Wildcard = bytes.Equal(data, []byte(`"*"`))
|
|
|
|
if !c.Wildcard {
|
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
|
|
|
|
}
|