Comments (2)
Got it, thank you. Sorry about not understanding the logic, the samples are not explaining in, and without a documentation that's explaining these things like you do it here, it's mostly trial and error.
from rapidyaml.
This is a continuation of #409.
All the behavior you describe is intended.
You seem to be laboring under a misunderstanding about what these functions do.
.has_val()
is purely a predicate to check if a node has a (possibly empty) scalar as its value..has_val()
is a structural query, and the logical opposite of.is_container()
in YAML structure; a container cannot be a val(scalar), and a val(scalar) cannot be a container. It tells you nothing about the contents of the scalar itself; only that it has a scalar and is therefore not a container. For example, in the YAML{a: }
for noderoot["a"]
,.has_val()
is true, even though its val is set to{nullptr, 0}
. But it is set, and this is the intent of the query.- when a node does not have a val, using
.val()
to get the val is UB. It causes an assertion in debug builds and gives you garbage in optimized builds. - likewise, if you ask whether
.val_is_null()
you will get the same assertion because- this check involves a string comparison against
"null"
,"~"
,"Null"
, etc - to get the val string, it needs to exist
- the node
.is_container()
, and does not have a val, so you can't read the val - hence the assertion you get when calling
.val_is_null()
when the node is a container
- this check involves a string comparison against
As for nullity, in the first example:
f
has the string"null"
as its scalar value- so it
.has_val()
- the val is a non-empty string, and has address. So comparing with
nullptr
yields false, because it is pointing at the string "null" which is non-null .val_is_null()
is indeed what you seem to be looking for. This function gets the scalar, and checks its contents against the preset list of null-meaning strings in YAML. But again, the subject node needs to verify.has_val()
, ie the subject node cannot be a container.
Consider this:
const Tree tree = parse_in_arena(R"(
a:
b: null
c: ~
)");
ConstNodeRef root = tree.rootref();
ConstNodeRef a = root["a"];
ConstNodeRef b = root["b"];
ConstNodeRef c = root["c"];
// the following holds:
assert(root.is_map());
assert(root.is_container());
assert(!root.has_val());
assert(a.has_val());
assert(b.has_val());
assert(c.has_val());
assert(a.val() == "");
assert(b.val() == "null");
assert(c.val() == "~");
assert(a.val() == nullptr);
assert(b.val() != nullptr);
assert(c.val() != nullptr);
assert(a.val_is_null());
assert(b.val_is_null());
assert(c.val_is_null());
assert(!root.val_is_null()); // ERROR! UB / crash. root is a container, cannot read the val.
So what you want to do is something like this:
// ...
ConstNodeRef f = _node["fermenter"]; // but ensure that "fermenter" exists!
if(f.is_container()) {
csubstr id = f["id"].val(); // but ensure that "id" exists!
}
else if( ! f.val_is_null()) {
csubstr id = f.val();
}
else {
// how to get the id from null?
}
from rapidyaml.
Related Issues (20)
- Request: Add more context to error traces HOT 3
- Tree::resolve() leaves some references in place in certain cases HOT 1
- Publish support for more platforms to Conan Central HOT 1
- How to get the correct col for a multiline string
- [Feature Request] Add an option to support customize installation HOT 2
- Rapidyaml calls abort(3) on random input HOT 3
- Question: filter utility use case HOT 3
- Error message 'closing ] not found' does not use _c4err macro
- An incomprehensible behavior occurred while using the for loop HOT 2
- Empty string instead of null serialized for nullptr HOT 1
- Namespace conflict with windows.h HOT 1
- Decimal precision truncated when serialize/deserialize std::vector<double> container HOT 2
- [Request] Make new release HOT 3
- [Bug] Text data with commas is incorrectly split HOT 4
- Serialize std::filesystem::path error HOT 16
- What is the best way to read files from ifstream in terms of performance. HOT 4
- SWIG API needs to raise exception
- How to get started when the docks are all about bragging and code examples are buried deep and inconsistent? HOT 1
- Unexpected behavior when deserializing to double HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rapidyaml.