2016-08-24 3 views
3

yaml-cpp를 사용하여 구문 분석하려는 테스트 yaml 파일이 있습니다. 나는 그것을 구문 분석은 yaml :: nodeType에 혼란 스럽습니다 : yaml-cpp로 정의되지 않았습니다.

test.yaml

testConfig: 
    # this points to additional config files to be parsed 
    includes: 
     required: "thing1.yaml" 
     optional: "thing2.yaml" 
    #some extraneous config information 
    foo: 42 
    bar: 394 
    baz: 8675309 

나는 testConfig.Type() 반환 YAML::NodeType::Map를 얻을. 그리고 이것은 예상되는 행동입니다.

그렇다면 includes.Type()YAML::NodeType::Undefined을 반환하므로 반복 할 수없는 필수 또는 선택적 값을 가져 오기 위해 포함을 구문 분석하려고합니다. 나는 정말로 yaml과 yaml-cpp에 대해 새로운 것이므로 나에게 잘못된 정보를 알려주는 도움을 주시면 감사하겠습니다.

파싱 코드 :

{includes and other such nonsense} 
      . 
      . 
      . 
YAML::Node configRoot = YAML::LoadFile(path.c_str()); 
if(configRoot.IsNull()) 
{ 
    SYSTEM_LOG_ERROR("Failed to load the config file: %s.", 
        path.c_str()); 
    return false; 
} 

YAML::Node includes = configRoot["includes"]; 
/* ^^^^^^^^^^^^^^^ 
* I believe that here lies the issue as includes is undefined and 
* therefore I cannot iterate over it. 
*/ 
for(auto it = include.begin(); it != include.end(); ++it) 
{ 
    // do some fantastically brilliant CS voodoo! 
} 
      . 
      . 
      . 
{ more C++ craziness to follow } 

해결책 : 나는 내가 필요에 따라 포함 구문 분석 할 수 있도록 불필요한 최상위 configTest를 제거했습니다.

+0

YAML 파일에서'thingX.yaml'을 따옴표로 묶을 필요가 없습니다. – Anthon

+0

@Anthon 정보를 제공해 주셔서 감사합니다. json에게 익숙한데, 여기에는 거의 모든 것이 필요합니다. – CompSciGuy139

답변

1

하지만,지도에서 최고 수준의 키 testConfig입니다. 대신 configRoot["testConfig"]을 사용하십시오.

+0

나는 이것을 발견하고 논평을하려고했다! 빠른 답변 감사합니다. 방금 그 여분의 최상위 레벨 testConfig를 제거했습니다. – CompSciGuy139

2

글쎄, 최상위 YAML 문서에는 실제로 includes이라는 키가 포함되어 있지 않습니다. 여기에는 testConfig 키만 포함됩니다.

// ... 
YAML::Node configRoot = YAML::LoadFile(path.c_str())["testConfig"]; 
// ... 

을 또는 testConfig가있는 경우 사용자가 명시 적으로 확인하려면 : 당신은 첫 번째에 액세스해야 당신은 configRoot["includes"]보고있는

// ... 
YAML::Node configRoot = YAML::LoadFile(path.c_str()); 
// do check her as in your code 
YAML:Node testConfig = configRoot["testConfig"]; 
// check if testConfig is a mapping here 
YAML::Node includes = testConfig["includes"]; 
// ... 
관련 문제