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

Table — 섹션, 중첩, inline

~12 min · toml, tables, sections

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

구조화된 config 작성하는 세 방법

표준 table — [section]

[name] 으로 시작하는 줄이 새 table 열어. 그 후의 모든 key (다음 table 헤더까지) 가 그 table 에 속함. 이름이 key 문법 따름 (bare 또는 quoted, 중첩엔 dotted).

중첩 table — [section.subsection]

table 헤더의 점이 중첩 만듦. [server.database]server table 안의 database table.

Inline table — { key = value, ... }

중괄호 table 이 한 줄에 맞음. 작은 inline 객체 (단일 점, 단일 config 값) 에 사용, full [section] 이 무겁게 느껴질 때. Inline table 은 self-contained — 한 번 작성하면 나중 [section] 헤더로 추가 못 함.

YAML 보다 순서 덜 중요

[a] 전에 [a.b] 작성, 또는 [a] 완전 생략 (암묵적으로 존재) 가능. 파서가 알아냄. 비용은 소스 순서로 파일 다시 읽기가 항상 파싱 구조와 일치하지 않음 — 유연성 위한 작은 비용.

'table 재정의 못 함': table 이 [server] 로 한 번 열리면 나중 [server] 다시 못 씀. table 의 모든 key 를 한 헤더 아래 둬. (Sub-table 과 array of table 은 다름 — 다음 lesson 다룸.)

Code

표준 table·toml
title = "My App"
version = "1.0"

[server]
host = "localhost"
port = 8000

[database]
url = "postgresql://localhost/pippa"
pool_size = 5
중첩 table·toml
[server]
host = "localhost"
port = 8000

[server.tls]
enabled = true
cert_file = "./certs/server.pem"

[server.tls.cipher]
list = ["AES256-GCM", "CHACHA20"]
minimum_version = "TLS1.2"
Inline table·toml
# 같은 데이터, 두 스타일
# 스타일 A — 표준 table:
[point]
x = 0.5
y = 1.7

# 스타일 B — inline table:
point = { x = 0.5, y = 1.7 }

# Inline table 이 배열 안에서 좋음:
endpoints = [
  { name = "login",   path = "/auth/login",  rate_limit = 60 },
  { name = "signup",  path = "/auth/signup", rate_limit = 5  },
  { name = "chat",    path = "/chat",        rate_limit = 30 },
]

External links

Exercise

4+ 단계 중첩 든 깊은 YAML config 골라. 같은 데이터를 중첩 table 헤더 ([a.b.c]) 사용해 TOML 로 작성. 둘 다 읽어. 깊이 중첩 config 엔 TOML 의 평탄하지만 prefix 된 스타일이 YAML 의 공백-주도 들여쓰기보다 자주 명확 — 한눈에 어느 sub-tree 에 값이 사는지 보임.

Progress

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

댓글 0

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

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