C.W.K.
Stream
Lesson 03 of 08 · published

스칼라 — string, number, boolean, null

~12 min · yaml, scalars, strings, norway

Level 0평문
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

YAML 이 스칼라 자동 타이핑; override 가능

bare 값 port: 8000 은 정수로 읽힘. port: "8000" 은 string 으로 읽힘. 파서가 bare 형태로 타입 추측. 이 편의가 YAML 의 가장 유명한 footgun 이기도.

다섯 스칼라 스타일

  • Bare (plain)name: Pippa. 파서가 타입 추측.
  • 작은따옴표name: 'Pippa'. 항상 string. 백슬래시 literal.
  • 큰따옴표name: "Pippa". 항상 string. JSON 스타일 escape (\n, \t, \u00e9) 지원.
  • Literal block (|) — 멀티라인, 줄바꿈 보존.
  • Folded block (>) — 멀티라인, 줄을 스페이스로 합침.

Boolean, Norway 문제

YAML 1.1 (많은 파서 기본, 1.22 이전 Kubernetes 포함) 이 22 unquoted string 을 boolean 으로 다룸: y, Y, yes, Yes, YES, n, N, no, No, NO, true, True, TRUE, false, False, FALSE, on, On, ON, off, Off, OFF. 그래서 country: NOcountry: False 로 읽힘. 수정: 키워드 같은 string 값 항상 quote. country: "NO" 가 string.

Null

네 가지 bare 형태 다 null 로 파싱: null, Null, NULL, ~. 빈 값 (콜론 뒤에 아무것도 없는 키) 도 null. literal string 'null' 원하면 quote: 'null'.

Norway 문제 (실제 사례): config 파일이 두 자 ISO 코드로 국가 나열. countries: [GB, IE, NO, DE]. YAML 1.1 파서가 NOFalse 로 변환. Norway 가 리스트에서 사라짐, 예약 시스템이 노르웨이 주소로 메일 조용히 안 보냄, 버그 찾는 데 몇 주. 키워드로 잘못 읽힐 수 있는 것 다 quote.

Code

다섯 스칼라 스타일·yaml
bare:    Pippa
single:  'Pippa'
double:  "Pippa"
literal: |
  This is line one.
  This is line two.
  Newlines preserved.
folded:  >
  This is one
  long sentence
  joined by spaces.
타입 추측 — bare 값·yaml
an_int:    42
a_float:   3.14
a_neg:     -17
sci:       6.022e23
a_bool:    true
a_null:    null
an_empty:                # also null
empty_str: ""            # explicit empty string
Norway 문제·yaml
# 위험 — YAML 1.1 에서 다 boolean 으로 파싱
country_a: NO        # → False
country_b: ON        # → True
flag:      yes       # → True

# 안전 — quote 해서 string 강제
country_a: "NO"
country_b: "ON"
flag:      "yes"
1.2 수정 (파서 지원하면)·yaml
%YAML 1.2
---
# YAML 1.2 에선 true/false/True/False/TRUE/FALSE 만 boolean.
# 'yes', 'no', 'on', 'off' 는 이제 plain string.
# 대부분 modern 파서가 1.2 기본; 옛것은 여전히 1.1 규칙.

External links

Exercise

두 자 국가 코드 리스트 (NO, ON, OFF 포함) 가진 YAML 파일 작성. PyYAML (yaml.safe_load) 로 파싱 — Norway 와 친구들이 boolean 됨. 이제 각각 작은따옴표로 감싸고 다시 파싱. diff 가 30 초 안 Norway 문제.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.