|
| 1 | +import '../annotations.dart'; |
| 2 | + |
| 3 | +class _Unset { |
| 4 | + const _Unset(); |
| 5 | +} |
| 6 | + |
| 7 | +const Object _unset = _Unset(); |
| 8 | + |
| 9 | +List<String>? _stringList(Object? raw) { |
| 10 | + if (raw == null) { |
| 11 | + return null; |
| 12 | + } |
| 13 | + if (raw is Iterable) { |
| 14 | + return raw.map((e) => e.toString()).toList(); |
| 15 | + } |
| 16 | + return null; |
| 17 | +} |
| 18 | + |
| 19 | +Map<String, Object?>? _asObjectMap(Object? raw) { |
| 20 | + if (raw == null) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + if (raw is Map) { |
| 24 | + return raw.map((k, v) => MapEntry(k.toString(), v)); |
| 25 | + } |
| 26 | + return null; |
| 27 | +} |
| 28 | + |
| 29 | +/// Remote Meilisearch instance in a [Network] topology. |
| 30 | +@RequiredMeiliServerVersion('1.37.0') |
| 31 | +class Remote { |
| 32 | + const Remote({ |
| 33 | + this.url, |
| 34 | + this.searchApiKey, |
| 35 | + this.writeApiKey, |
| 36 | + }); |
| 37 | + |
| 38 | + final String? url; |
| 39 | + final String? searchApiKey; |
| 40 | + final String? writeApiKey; |
| 41 | + |
| 42 | + factory Remote.fromMap(Map<String, Object?> map) { |
| 43 | + return Remote( |
| 44 | + url: map['url'] as String?, |
| 45 | + searchApiKey: map['searchApiKey'] as String?, |
| 46 | + writeApiKey: map['writeApiKey'] as String?, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /// Fields to send in a PATCH body (sparse). |
| 51 | + Map<String, Object?> toPatchMap() { |
| 52 | + return <String, Object?>{ |
| 53 | + if (url != null) 'url': url, |
| 54 | + if (searchApiKey != null) 'searchApiKey': searchApiKey, |
| 55 | + if (writeApiKey != null) 'writeApiKey': writeApiKey, |
| 56 | + }; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Shard ownership configuration within a [Network]. |
| 61 | +@RequiredMeiliServerVersion('1.37.0') |
| 62 | +class Shard { |
| 63 | + const Shard({ |
| 64 | + this.remotes, |
| 65 | + this.addRemotes, |
| 66 | + this.removeRemotes, |
| 67 | + }); |
| 68 | + |
| 69 | + final List<String>? remotes; |
| 70 | + final List<String>? addRemotes; |
| 71 | + final List<String>? removeRemotes; |
| 72 | + |
| 73 | + factory Shard.fromMap(Map<String, Object?> map) { |
| 74 | + return Shard( |
| 75 | + remotes: _stringList(map['remotes']), |
| 76 | + addRemotes: _stringList(map['addRemotes']), |
| 77 | + removeRemotes: _stringList(map['removeRemotes']), |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + Map<String, Object?> toPatchMap() { |
| 82 | + return <String, Object?>{ |
| 83 | + if (remotes != null) 'remotes': remotes, |
| 84 | + if (addRemotes != null) 'addRemotes': addRemotes, |
| 85 | + if (removeRemotes != null) 'removeRemotes': removeRemotes, |
| 86 | + }; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Network topology returned by `GET /network` / `PATCH /network`. |
| 91 | +@RequiredMeiliServerVersion('1.37.0') |
| 92 | +class Network { |
| 93 | + const Network({ |
| 94 | + this.self, |
| 95 | + this.leader, |
| 96 | + this.remotes, |
| 97 | + this.shards, |
| 98 | + }); |
| 99 | + |
| 100 | + final String? self; |
| 101 | + final String? leader; |
| 102 | + final Map<String, Remote>? remotes; |
| 103 | + final Map<String, Shard>? shards; |
| 104 | + |
| 105 | + factory Network.fromMap(Map<String, Object?> json) { |
| 106 | + Map<String, Remote>? remotes; |
| 107 | + final remotesRaw = _asObjectMap(json['remotes']); |
| 108 | + if (remotesRaw != null) { |
| 109 | + remotes = remotesRaw.map( |
| 110 | + (k, v) => MapEntry( |
| 111 | + k, |
| 112 | + Remote.fromMap(Map<String, Object?>.from(v! as Map)), |
| 113 | + ), |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + Map<String, Shard>? shards; |
| 118 | + final shardsRaw = _asObjectMap(json['shards']); |
| 119 | + if (shardsRaw != null) { |
| 120 | + shards = shardsRaw.map( |
| 121 | + (k, v) => MapEntry( |
| 122 | + k, |
| 123 | + Shard.fromMap(Map<String, Object?>.from(v! as Map)), |
| 124 | + ), |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + return Network( |
| 129 | + self: json['self'] as String?, |
| 130 | + leader: json['leader'] as String?, |
| 131 | + remotes: remotes, |
| 132 | + shards: shards, |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/// Partial update payload for `PATCH /network`. |
| 138 | +/// |
| 139 | +/// Omit a field to leave it unchanged. Pass [leader] or [self] as `null` to |
| 140 | +/// clear. For [remotes] / [shards], pass a map; use `null` values for entries |
| 141 | +/// to remove a remote or shard. |
| 142 | +@RequiredMeiliServerVersion('1.37.0') |
| 143 | +class UpdateNetworkOptions { |
| 144 | + UpdateNetworkOptions({ |
| 145 | + Object? self = _unset, |
| 146 | + Object? leader = _unset, |
| 147 | + Object? remotes = _unset, |
| 148 | + Object? shards = _unset, |
| 149 | + }) : _self = self, |
| 150 | + _leader = leader, |
| 151 | + _remotes = remotes, |
| 152 | + _shards = shards; |
| 153 | + |
| 154 | + final Object? _self; |
| 155 | + final Object? _leader; |
| 156 | + final Object? _remotes; |
| 157 | + final Object? _shards; |
| 158 | + |
| 159 | + Map<String, Object?> toPatchMap() { |
| 160 | + final out = <String, Object?>{}; |
| 161 | + if (!identical(_self, _unset)) { |
| 162 | + out['self'] = _self as String?; |
| 163 | + } |
| 164 | + if (!identical(_leader, _unset)) { |
| 165 | + out['leader'] = _leader as String?; |
| 166 | + } |
| 167 | + if (!identical(_remotes, _unset)) { |
| 168 | + final m = _remotes as Map<String, Remote?>?; |
| 169 | + if (m != null) { |
| 170 | + out['remotes'] = m.map( |
| 171 | + (k, v) => MapEntry( |
| 172 | + k, |
| 173 | + v?.toPatchMap(), |
| 174 | + ), |
| 175 | + ); |
| 176 | + } else { |
| 177 | + out['remotes'] = null; |
| 178 | + } |
| 179 | + } |
| 180 | + if (!identical(_shards, _unset)) { |
| 181 | + final m = _shards as Map<String, Shard?>?; |
| 182 | + if (m != null) { |
| 183 | + out['shards'] = m.map( |
| 184 | + (k, v) => MapEntry( |
| 185 | + k, |
| 186 | + v?.toPatchMap(), |
| 187 | + ), |
| 188 | + ); |
| 189 | + } else { |
| 190 | + out['shards'] = null; |
| 191 | + } |
| 192 | + } |
| 193 | + return out; |
| 194 | + } |
| 195 | +} |
0 commit comments