From f22715063c8d2a7a145b0261be7a5d1e688e38dd Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Thu, 16 Jul 2020 13:50:45 -0700 Subject: [PATCH] allow Union deref w/o field name --- src/data.cpp | 14 ++++++++++++-- test/testdata.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/data.cpp b/src/data.cpp index d618e4e..1fd8074 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -715,7 +715,7 @@ void Value::traverse(const std::string &expr, bool modify, bool dothrow) maybedot = false; - if(expr.size()-pos >= 3 && expr[pos]=='-' && expr[pos+1]=='>') { + if(expr.size()-pos >= 2 && expr[pos]=='-' && expr[pos+1]=='>') { pos += 2; // skip past "->" if(desc->code.code==TypeCode::Any) { @@ -727,10 +727,10 @@ void Value::traverse(const std::string &expr, bool modify, bool dothrow) size_t sep = expr.find_first_of("<[-.", pos); decltype (desc->mlookup)::const_iterator it; + auto& fld = store->as(); if(sep>0 && (it=desc->mlookup.find(expr.substr(pos, sep-pos)))!=desc->mlookup.end()) { // found it. - auto& fld = store->as(); if(modify || fld.desc==&desc->members[it->second]) { // will select, or already selected @@ -750,6 +750,16 @@ void Value::traverse(const std::string &expr, bool modify, bool dothrow) if(dothrow) throw LookupError(SB()<<"traversing const Value, can't select Union in '"<u16"] = 42; + testEq(val.as(), "42"); + val["->s"] = "test"; + testEq(val.as(), "test"); + + testEq(val.nameOf(val["->"]), "s"); + + testThrows([&val](){ + val["->u16"] = "hello"; + }); + + //val = nullptr; +} + void testName() { testDiag("%s", __func__); @@ -314,10 +337,11 @@ void testAssignSimilar() MAIN(testdata) { - testPlan(93); + testPlan(97); testSetup(); testTraverse(); testAssign(); + testAssignUnion(); testName(); testIterStruct(); testIterUnion();