Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [diff] [blame^] | 1 | package sarama |
| 2 | |
| 3 | type DescribeGroupsResponse struct { |
| 4 | Groups []*GroupDescription |
| 5 | } |
| 6 | |
| 7 | func (r *DescribeGroupsResponse) encode(pe packetEncoder) error { |
| 8 | if err := pe.putArrayLength(len(r.Groups)); err != nil { |
| 9 | return err |
| 10 | } |
| 11 | |
| 12 | for _, groupDescription := range r.Groups { |
| 13 | if err := groupDescription.encode(pe); err != nil { |
| 14 | return err |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | return nil |
| 19 | } |
| 20 | |
| 21 | func (r *DescribeGroupsResponse) decode(pd packetDecoder, version int16) (err error) { |
| 22 | n, err := pd.getArrayLength() |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | |
| 27 | r.Groups = make([]*GroupDescription, n) |
| 28 | for i := 0; i < n; i++ { |
| 29 | r.Groups[i] = new(GroupDescription) |
| 30 | if err := r.Groups[i].decode(pd); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | func (r *DescribeGroupsResponse) key() int16 { |
| 39 | return 15 |
| 40 | } |
| 41 | |
| 42 | func (r *DescribeGroupsResponse) version() int16 { |
| 43 | return 0 |
| 44 | } |
| 45 | |
| 46 | func (r *DescribeGroupsResponse) requiredVersion() KafkaVersion { |
| 47 | return V0_9_0_0 |
| 48 | } |
| 49 | |
| 50 | type GroupDescription struct { |
| 51 | Err KError |
| 52 | GroupId string |
| 53 | State string |
| 54 | ProtocolType string |
| 55 | Protocol string |
| 56 | Members map[string]*GroupMemberDescription |
| 57 | } |
| 58 | |
| 59 | func (gd *GroupDescription) encode(pe packetEncoder) error { |
| 60 | pe.putInt16(int16(gd.Err)) |
| 61 | |
| 62 | if err := pe.putString(gd.GroupId); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | if err := pe.putString(gd.State); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | if err := pe.putString(gd.ProtocolType); err != nil { |
| 69 | return err |
| 70 | } |
| 71 | if err := pe.putString(gd.Protocol); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | if err := pe.putArrayLength(len(gd.Members)); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | for memberId, groupMemberDescription := range gd.Members { |
| 80 | if err := pe.putString(memberId); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if err := groupMemberDescription.encode(pe); err != nil { |
| 84 | return err |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | func (gd *GroupDescription) decode(pd packetDecoder) (err error) { |
| 92 | kerr, err := pd.getInt16() |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | |
| 97 | gd.Err = KError(kerr) |
| 98 | |
| 99 | if gd.GroupId, err = pd.getString(); err != nil { |
| 100 | return |
| 101 | } |
| 102 | if gd.State, err = pd.getString(); err != nil { |
| 103 | return |
| 104 | } |
| 105 | if gd.ProtocolType, err = pd.getString(); err != nil { |
| 106 | return |
| 107 | } |
| 108 | if gd.Protocol, err = pd.getString(); err != nil { |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | n, err := pd.getArrayLength() |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | if n == 0 { |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | gd.Members = make(map[string]*GroupMemberDescription) |
| 121 | for i := 0; i < n; i++ { |
| 122 | memberId, err := pd.getString() |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | |
| 127 | gd.Members[memberId] = new(GroupMemberDescription) |
| 128 | if err := gd.Members[memberId].decode(pd); err != nil { |
| 129 | return err |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return nil |
| 134 | } |
| 135 | |
| 136 | type GroupMemberDescription struct { |
| 137 | ClientId string |
| 138 | ClientHost string |
| 139 | MemberMetadata []byte |
| 140 | MemberAssignment []byte |
| 141 | } |
| 142 | |
| 143 | func (gmd *GroupMemberDescription) encode(pe packetEncoder) error { |
| 144 | if err := pe.putString(gmd.ClientId); err != nil { |
| 145 | return err |
| 146 | } |
| 147 | if err := pe.putString(gmd.ClientHost); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | if err := pe.putBytes(gmd.MemberMetadata); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | if err := pe.putBytes(gmd.MemberAssignment); err != nil { |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | func (gmd *GroupMemberDescription) decode(pd packetDecoder) (err error) { |
| 161 | if gmd.ClientId, err = pd.getString(); err != nil { |
| 162 | return |
| 163 | } |
| 164 | if gmd.ClientHost, err = pd.getString(); err != nil { |
| 165 | return |
| 166 | } |
| 167 | if gmd.MemberMetadata, err = pd.getBytes(); err != nil { |
| 168 | return |
| 169 | } |
| 170 | if gmd.MemberAssignment, err = pd.getBytes(); err != nil { |
| 171 | return |
| 172 | } |
| 173 | |
| 174 | return nil |
| 175 | } |
| 176 | |
| 177 | func (gmd *GroupMemberDescription) GetMemberAssignment() (*ConsumerGroupMemberAssignment, error) { |
| 178 | assignment := new(ConsumerGroupMemberAssignment) |
| 179 | err := decode(gmd.MemberAssignment, assignment) |
| 180 | return assignment, err |
| 181 | } |
| 182 | |
| 183 | func (gmd *GroupMemberDescription) GetMemberMetadata() (*ConsumerGroupMemberMetadata, error) { |
| 184 | metadata := new(ConsumerGroupMemberMetadata) |
| 185 | err := decode(gmd.MemberMetadata, metadata) |
| 186 | return metadata, err |
| 187 | } |