본문 바로가기
software engineering

yaml - to quote or not to quote?

by _블로그 2021. 2. 18.

yaml의 string을 표현할 때 quote(single or double)를 사용해야 하는가? 사용해야 하면 single or double?

www.yaml.info/learn/quote.html

 

Quoting - Learn - yaml.info

To Quote or not to Quote? This article covers scalar styles in YAML 1.1 and 1.2. It mostly works the same in both versions. One design goal of YAML was that it's human friendly. It should be easy to read and edit, even if that makes parsing it harder. Let'

www.yaml.info

YAML의 목표는 사람이 읽기 좋은 포맷이다. 설사 parsing이 좀 어려워진다고해도, readability에 중점을 둔 포맷이라는 얘기. quoting도 가독성을 중점으로 살펴봐야하는게 맞다. 결국은 multiline과 escaping을 어떻게 가독성있게 잘 처리할 것인가라는게 중점이란 말이지.

tl;dr

quoting 없이 사용하는 것을 기본으로 하되, 특수문자가 사용된다거나 YAML syntax와 헷갈린다 싶으면 double quoting.

multiline string이 필요하면 Literal Block Scalar로.

Flow Scalars

flow라는 말답게 물 흐르듯 연속적으로 인식하는 scalars 이다. 시작과 끝의 whitespace는 무시되고 newline은 space로 대체되어 합쳐진다. quoting 없이 그냥 쓰는 plain scalars와 quoting(single, double)하는 scalars로 나뉜다.

multi: this is
   all one
   single line

위랑 아래랑 같다

same as: this is all one single line

Plain Scalars

기본적으로 YAML은 qutoes가 필요없다. 탭이나 백슬래시 그리고 유니코드도 그냥 쓰면 됨. 다만 탭은 탭이라고 눈에 딱 안들어오잖아? 사용하지 않는 걸 권장해. \n이나 \t같은 escapes sequences는 모두 parsing되지 않고 string으로 인식할꺼야.

 

그리고 주석은 plain scalar 맨 뒤에 위치하는데, 아래처럼 멀티라인이라고 해도 하나의 plain scalar이기 때문에 invalid해.

multi:
  first    # a comment
  second   # this is invalid

아래처럼 맨 끝에 주석 하나만 있어야 되.

multi:
  first
  second   # a comment

그리고 -<space>는 plain scalar 처음에 사용할 순 없지만, 줄바꿈한 뒤에는 사용할 수 있어. 모두 하나의 string으로 처리하니까 줄바꿈한 뒤 -<space>는 그냥 string으로 처리할테니. 그런데 이거 list랑 헷갈리잖아? 하지말자.

- a multiline
  - plain string

# same as
- "a multiline - plain string"

위에랑 아래랑 똑같은데 위에처럼 쓰지 말자.

When not te use Plain Scalars

qutoes가 필요없다하지만 YAML syntax랑 겹치면 당연히 못쓰지 않겠어? 맨 앞에 아래 애들 못 쓰는데, 그냥 특수문자는 다 못쓴다고 생각하자.

!, &, *, -<space>, :<space>, ?<space>, {, }, [, ], <comma>, #, |, >, @, `, ", ', <whitespace>, %

:<space>는 key/value 구분자라서, <space>#은 comment의 시작이라 plain scalar 안에선 다 안돼.

 

그 밖에도 여러 제약이 있는데 제약도 YAML 구현체에 따라 왔다갔다 하니 정석대로 사용하자고.

# Some processors don't implement this correctly. To be
# sure you should always add a space.
flow mapping: {key:[sequence]}

위에 key: 다음에 <space>가 없는데, 구현체에 따라 바르게 인식하기도 안하기도 해. :<space>는 지켜주자.

Special types

아래는 boolean이나 number, 혹은 null로 인식될 수 있으니 당연히 plain으로 못해.

  • true, false
  • 23
  • 1e3
  • 3.14159
  • null

Single Quoted Scalar

특수문자가 허용된다는 것외에는 대부분 plain이랑 같아. single quote는 그럼 어떻게 escpae하나? 아래처럼 두개 써주면 됨.

a string: 'with one single '' quote'

multiple lines에서는 plain과 달리 whitespace를 trim해주지 않아. 예상대로.

multi:
  '  a
  b
  c
  d   '
single: "  a b c d   "

Double Quoted Scalar

single quoted scalar에 escape sequences 처리가 추가된 버전이야. "\n"은 newline으로 인식된다 이 말이지. escaping rules는 JSON과 거의 호환되. double quote의 escaping은 \" 으로 가능해. single은 '', double은 \" 아놔.

 

맨 끝에 backslash는 shell script의 그것과 유사하게 다음 라인 앞의 모든 whitespace를 제거해줘.

a long string without spaces:
  "loooooooooooooooooooooooooooooooooooooooooooooooooongstring\
   loooooooooooooooooooooooooooooooooooooooooooooooooongstring\
   loooooooooooooooooooooooooooooooooooooooooooooooooongstring"

Block Scalars

string이 길다면 block scalar를 사용하는 것도 좋은 선택이야. 어떤 character sequence도 사용 가능해. escape sequences는 사용할 수 없음. Literal, Folded 두가지가 있음.

Literal Block Scalar

 |  pipe로 시작해. 컨텐츠는 다음 라인부터 시작하고 indent가 필수야.

literal: |
  line 1
   line 2
  end

parser가 맨 처음 indent를 찾고 그 indent 기준으로 인식해. 위는 아래와 같아.

quoted: "line 1\n line 2\nend\n"

bash, json, 심지어 yaml도 embed 할 수 있어. json에서 이걸 한다고 생각해봐. 어휴. 끔찍하지.

Folded Block Scalar

block scalar인데 이름에서 알 수 있듯이 newline은 space로 folded 해줘. > sign으로 시작하면 됨.

a long command: >
  apt-get update
  && apt-get install -y
  git tig vim jq tmux tmate git-subrepo
quoted: "apt-get update && apt-get install -y git
  tig vim jq tmux tmate git-subrepo\n"

? 맨 끝에 \n은 왜 붙는거지? 아무튼 newline을 엔터 두번으로 입력할 수 있다는 것외에는 Double quoted scalar와 동일한 것 같은데? 이건 헷갈리니 그냥 쓰지 말자.

LIST

'software engineering' 카테고리의 다른 글

IntelliJ terminal의 vi 한글 깨짐  (0) 2021.04.13

댓글