github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/smartcontract/manifest/container.go (about) 1 package manifest 2 3 // This file contains types and helper methods for wildcard containers. 4 // A wildcard container can contain either a finite set of elements or 5 // every possible element, in which case it is named `wildcard`. 6 7 import ( 8 "bytes" 9 "encoding/json" 10 ) 11 12 // WildStrings represents a string set which can be a wildcard. 13 type WildStrings struct { 14 Value []string 15 } 16 17 // WildPermissionDescs represents a PermissionDescriptor set which can be a wildcard. 18 type WildPermissionDescs struct { 19 Value []PermissionDesc 20 } 21 22 // Contains checks if v is in the container. 23 func (c *WildStrings) Contains(v string) bool { 24 if c.IsWildcard() { 25 return true 26 } 27 for _, s := range c.Value { 28 if v == s { 29 return true 30 } 31 } 32 return false 33 } 34 35 // Contains checks if v is in the container. 36 func (c *WildPermissionDescs) Contains(v PermissionDesc) bool { 37 if c.IsWildcard() { 38 return true 39 } 40 for _, u := range c.Value { 41 if u.Equals(v) { 42 return true 43 } 44 } 45 return false 46 } 47 48 // IsWildcard returns true iff the container is a wildcard. 49 func (c *WildStrings) IsWildcard() bool { return c.Value == nil } 50 51 // IsWildcard returns true iff the container is a wildcard. 52 func (c *WildPermissionDescs) IsWildcard() bool { return c.Value == nil } 53 54 // Restrict transforms the container into an empty one. 55 func (c *WildStrings) Restrict() { c.Value = []string{} } 56 57 // Restrict transforms the container into an empty one. 58 func (c *WildPermissionDescs) Restrict() { c.Value = []PermissionDesc{} } 59 60 // Add adds v to the container. 61 func (c *WildStrings) Add(v string) { c.Value = append(c.Value, v) } 62 63 // Add adds v to the container. 64 func (c *WildPermissionDescs) Add(v PermissionDesc) { c.Value = append(c.Value, v) } 65 66 // MarshalJSON implements the json.Marshaler interface. 67 func (c WildStrings) MarshalJSON() ([]byte, error) { 68 if c.IsWildcard() { 69 return []byte(`"*"`), nil 70 } 71 return json.Marshal(c.Value) 72 } 73 74 // MarshalJSON implements the json.Marshaler interface. 75 func (c WildPermissionDescs) MarshalJSON() ([]byte, error) { 76 if c.IsWildcard() { 77 return []byte(`"*"`), nil 78 } 79 return json.Marshal(c.Value) 80 } 81 82 // UnmarshalJSON implements the json.Unmarshaler interface. 83 func (c *WildStrings) UnmarshalJSON(data []byte) error { 84 if !bytes.Equal(data, []byte(`"*"`)) { 85 ss := []string{} 86 if err := json.Unmarshal(data, &ss); err != nil { 87 return err 88 } 89 c.Value = ss 90 } 91 return nil 92 } 93 94 // UnmarshalJSON implements the json.Unmarshaler interface. 95 func (c *WildPermissionDescs) UnmarshalJSON(data []byte) error { 96 if !bytes.Equal(data, []byte(`"*"`)) { 97 us := []PermissionDesc{} 98 if err := json.Unmarshal(data, &us); err != nil { 99 return err 100 } 101 c.Value = us 102 } 103 return nil 104 }