Skip to content

Commit 3d86c39

Browse files
committed
CodesHandle::getBytes: use length instead of size to determine array size
1 parent c381622 commit 3d86c39

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

src/metkit/codes/GRIBDecoder.cc

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,14 @@ void GRIBDecoder::getMetadata(const eckit::message::Message& msg, eckit::message
6060
break;
6161
}
6262
case eckit::message::ValueRepresentation::Native: {
63-
// https://jira.ecmwf.int/browse/ECC-2166
64-
if (name == "uuidOfHGrid") {
65-
// uuidOfHGrid returns size 1 although it contains 16 bytes
66-
gather.setValue(name, k.getString());
67-
}
68-
else {
69-
std::visit(
70-
[&](auto&& v) {
71-
using Type = std::decay_t<decltype(v)>;
72-
if constexpr (std::is_same_v<Type, std::string> || std::is_arithmetic_v<Type>) {
73-
gather.setValue(name, std::forward<decltype(v)>(v));
74-
}
75-
},
76-
k.get());
77-
}
63+
std::visit(
64+
[&](auto&& v) {
65+
using Type = std::decay_t<decltype(v)>;
66+
if constexpr (std::is_same_v<Type, std::string> || std::is_arithmetic_v<Type>) {
67+
gather.setValue(name, std::forward<decltype(v)>(v));
68+
}
69+
},
70+
k.get());
7871
break;
7972
}
8073
}

src/metkit/codes/api/CodesAPI.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class OwningCodesHandle : public CodesHandle {
8585
void forceSet(const std::string& key, Span<const double> value) override;
8686
void forceSet(const std::string& key, Span<const float> value) override;
8787
size_t size(const std::string& key) const override;
88+
size_t length(const std::string& key) const override;
8889
CodesValue get(const std::string& key) const override;
8990
NativeType type(const std::string& key) const override;
9091
long getLong(const std::string& key) const override;
@@ -214,6 +215,12 @@ size_t OwningCodesHandle::size(const std::string& key) const {
214215
return size;
215216
}
216217

218+
size_t OwningCodesHandle::length(const std::string& key) const {
219+
size_t length;
220+
throwOnError(codes_get_length(raw(), key.c_str(), &length), Here(), "CodesHandle::length(string)", key);
221+
return length;
222+
}
223+
217224
/// Get the value of the key
218225
CodesValue OwningCodesHandle::get(const std::string& key) const {
219226
NativeType ktype = type(key);
@@ -345,7 +352,7 @@ std::vector<std::string> OwningCodesHandle::getStringArray(const std::string& ke
345352

346353
std::vector<uint8_t> OwningCodesHandle::getBytes(const std::string& key) const {
347354
std::vector<uint8_t> ret;
348-
std::size_t ksize = size(key);
355+
std::size_t ksize = length(key);
349356
ret.resize(ksize);
350357
throwOnError(codes_get_bytes(raw(), key.c_str(), ret.data(), &ksize), Here(), "CodesHandle::getBytes(string)", key);
351358
ret.resize(ksize);

src/metkit/codes/api/CodesAPI.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ class CodesHandle {
203203
/// @return For given scalars 1 is returned. For given arrays the size of the array is returned..
204204
virtual size_t size(const std::string& key) const = 0;
205205

206+
/// Returns the number of chars/bytes contained for a given key
207+
// of type string/byte array.
208+
///
209+
/// Can be used to determine if a field is storing a scalar or an array.
210+
/// @param key Name of the field that is supposed to be inspected.
211+
/// @return Number of chars/bytes of contained string/byte array
212+
virtual size_t length(const std::string& key) const = 0;
213+
206214
/// Get the value of the key.
207215
///
208216
/// High-level functionality:

0 commit comments

Comments
 (0)