![article thumbnail image](https://blog.kakaocdn.net/dn/cLm4qi/btq64SCljGC/iEQMD9UhkPUC4dvcEev0Ck/img.png)
YAML 기본 문법
YAML?
YAML(Yet Another Markup Language)
.yml, .yaml
들여쓰기 (indent)
2칸(권장) 또는 4칸
person:
name: Nabin Kim
job: developer
skills:
- docker
- kubernetes
데이터 정의 (map)
key: value
apiVersion: v1
kind: Pod
metadata:
name: echo
labels:
type: app
배열 정의 (array)
- 로 표시
person:
name: Nabin Kim
job: Developer
skills:
- docker
- kubernetes
주석 (comment)
# 로 표시
# comment
name: Nabin Kim # comment
참/거짓
true, false, yes, no
study_hard: yes
give_up: no
hello: True
word: TRUE
manual: false
숫자 표현
# number
version: 1.2
# string
version: "1.2"
줄 바꿈
마지막 줄바꿈을 포함하는 | 지시어
newlines_sample: |
number one line
second line
last line
# 이 라인 포함 (number one line\n\nsecond line\n\nlast line\n)
마지막 줄바꿈을 제외하는 |- 지시어
newlines_sample: |-
number one line
second line
last line
# 이 라인 미포함 (number one line\n\nsecond line\n\nlast line)
중간에 들어간 빈줄을 제외하는 > 지시어
newlines_sample: >
number one line
second line
last line
# 중간 공백 미포함 (number one line\nsecond line\nlast line\n)
주의사항
key와 value의 구분
key와 value 사이에는 반드시 빈칸이 필요하다.
# error (not key-value, string)
key:value
# ok!
key: value
문자열 따옴표
대부분의 문자열을 따옴표 없이 사용해도 무방하나, key 값에 :가 들어간 경우에는 반드시 따옴표가 필요하다.
*, - 등이 앞에 올 때도 따옴표를 사용해야 한다.
# error
windows_drive: c:
# ok!
windows_drive: "c:"
windows_drive: 'c:'
yaml-cpp
https://github.com/jbeder/yaml-cpp
jbeder/yaml-cpp
A YAML parser and emitter in C++. Contribute to jbeder/yaml-cpp development by creating an account on GitHub.
github.com
Win32 빌드
cmake -G "Visual Studio 16 2019" -A Win32 ..
yaml-cpp in Qt
yaml-cpp의 Qt 3rd party 사용 (qtyaml.h)
https://gist.github.com/brcha/d392b2fe5f1e427cc8a6
Qt Yaml support using yaml-cpp library
Qt Yaml support using yaml-cpp library. GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
예제
yaml file
start:
- 1
- 3
- 0
end:
point: 1
test:
- 'Hello'
- 'World'
in cpp
try
{
YAML::Node node = YAML::LoadFile("C:/File/Path/test.yml");
auto str = node["start"].as<QVector<int>>();
auto test = node["nothing"];
auto str2 = node["end"].as<QMap<QString, YAML::Node>>();
if (!test.IsDefined() || test.IsNull())
{
// 내용이 없는 경우는 null, 아예 key 자체를 찾지 못한 경우는 undefined
qDebug() << "Nothing.";
}
else
{
qDebug() << (YAML::NodeType::value)test.Type();
}
qDebug() << str << str2["point"].as<int>() << str2["test"].as<QVector<QString>>();
}
catch (YAML::cast_exception &e)
{
// error 처리
}
output
14:37:57 [DEBUG] Nothing.
14:37:57 [DEBUG] QVector(1, 3, 0) 1 QVector("Hello", "World")
참고 사이트
https://www.inflearn.com/questions/16184
yaml파일 이란 무엇인가요 - 인프런 | 질문 & 답변
안녕하세요 강사님 너무 질문이 많아서 죄송합니다. yaml파일 이라는 단어를 요 근래 많이 듣고 있는데 정확인 무슨 파일인가요 검색해 보지도 않고 무조건 질문을 드리는것 같아서 죄송하지만
www.inflearn.com
https://subicura.com/k8s/prepare/yaml.html
YAML 문법
YAML 문법에 대해 전반적으로 알아봅니다.
subicura.com