|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2015-2025 MinIO, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package set |
| 19 | + |
| 20 | +import "github.com/tinylib/msgp/msgp" |
| 21 | + |
| 22 | +// EncodeMsg encodes the message to the writer. |
| 23 | +// Values are stored as a slice of strings or nil. |
| 24 | +func (s StringSet) EncodeMsg(writer *msgp.Writer) error { |
| 25 | + if s == nil { |
| 26 | + return writer.WriteNil() |
| 27 | + } |
| 28 | + err := writer.WriteArrayHeader(uint32(len(s))) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + sorted := s.ToByteSlices() |
| 33 | + for _, k := range sorted { |
| 34 | + err = writer.WriteStringFromBytes(k) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + } |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +// MarshalMsg encodes the message to the bytes. |
| 43 | +// Values are stored as a slice of strings or nil. |
| 44 | +func (s StringSet) MarshalMsg(bytes []byte) ([]byte, error) { |
| 45 | + if s == nil { |
| 46 | + return msgp.AppendNil(bytes), nil |
| 47 | + } |
| 48 | + if len(s) == 0 { |
| 49 | + return msgp.AppendArrayHeader(bytes, 0), nil |
| 50 | + } |
| 51 | + bytes = msgp.AppendArrayHeader(bytes, uint32(len(s))) |
| 52 | + sorted := s.ToByteSlices() |
| 53 | + for _, k := range sorted { |
| 54 | + bytes = msgp.AppendStringFromBytes(bytes, k) |
| 55 | + } |
| 56 | + return bytes, nil |
| 57 | +} |
| 58 | + |
| 59 | +// DecodeMsg decodes the message from the reader. |
| 60 | +func (s *StringSet) DecodeMsg(reader *msgp.Reader) error { |
| 61 | + if reader.IsNil() { |
| 62 | + *s = nil |
| 63 | + return reader.Skip() |
| 64 | + } |
| 65 | + sz, err := reader.ReadArrayHeader() |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + dst := *s |
| 70 | + if dst == nil { |
| 71 | + dst = make(StringSet, sz) |
| 72 | + } else { |
| 73 | + for k := range dst { |
| 74 | + delete(dst, k) |
| 75 | + } |
| 76 | + } |
| 77 | + for i := uint32(0); i < sz; i++ { |
| 78 | + var k string |
| 79 | + k, err = reader.ReadString() |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + dst[k] = struct{}{} |
| 84 | + } |
| 85 | + *s = dst |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// UnmarshalMsg decodes the message from the bytes. |
| 90 | +func (s *StringSet) UnmarshalMsg(bytes []byte) ([]byte, error) { |
| 91 | + if msgp.IsNil(bytes) { |
| 92 | + *s = nil |
| 93 | + return bytes[msgp.NilSize:], nil |
| 94 | + } |
| 95 | + // Read the array header |
| 96 | + sz, bytes, err := msgp.ReadArrayHeaderBytes(bytes) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + dst := *s |
| 101 | + if dst == nil { |
| 102 | + dst = make(StringSet, sz) |
| 103 | + } else { |
| 104 | + for k := range dst { |
| 105 | + delete(dst, k) |
| 106 | + } |
| 107 | + } |
| 108 | + for i := uint32(0); i < sz; i++ { |
| 109 | + var k string |
| 110 | + k, bytes, err = msgp.ReadStringBytes(bytes) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + dst[k] = struct{}{} |
| 115 | + } |
| 116 | + *s = dst |
| 117 | + return bytes, nil |
| 118 | +} |
| 119 | + |
| 120 | +// Msgsize returns the maximum size of the message. |
| 121 | +func (s StringSet) Msgsize() int { |
| 122 | + if s == nil { |
| 123 | + return msgp.NilSize |
| 124 | + } |
| 125 | + if len(s) == 0 { |
| 126 | + return msgp.ArrayHeaderSize |
| 127 | + } |
| 128 | + size := msgp.ArrayHeaderSize |
| 129 | + for key := range s { |
| 130 | + size += msgp.StringPrefixSize + len(key) |
| 131 | + } |
| 132 | + return size |
| 133 | +} |
| 134 | + |
| 135 | +// MarshalBinary encodes the receiver into a binary form and returns the result. |
| 136 | +func (s StringSet) MarshalBinary() ([]byte, error) { |
| 137 | + return s.MarshalMsg(nil) |
| 138 | +} |
| 139 | + |
| 140 | +// AppendBinary appends the binary representation of itself to the end of b |
| 141 | +func (s StringSet) AppendBinary(b []byte) ([]byte, error) { |
| 142 | + return s.MarshalMsg(b) |
| 143 | +} |
| 144 | + |
| 145 | +// UnmarshalBinary decodes the binary representation of itself from b |
| 146 | +func (s *StringSet) UnmarshalBinary(b []byte) error { |
| 147 | + _, err := s.UnmarshalMsg(b) |
| 148 | + return err |
| 149 | +} |
0 commit comments