อัปเดตล่าสุด: เมษายน 2569
TOML Formatting คืออะไร?
TOML (Tom's Obvious Minimal Language) คือรูปแบบไฟล์ config ที่สร้างโดย Tom Preston-Werner ในปี 2013 มันแมปตรงกับ hash table และใช้การกำหนดประเภทที่ชัดเจนสำหรับทุก value ตัวจัดรูปแบบ TOML รับ TOML ดิบหรือที่มีรูปแบบไม่สม่ำเสมอแล้ว serialize ใหม่ด้วย spacing ที่สม่ำเสมอ indentation ที่ถูกต้อง และการจัดลำดับ key แบบ canonical ผลลัพธ์คือไฟล์ที่ปฏิบัติตามข้อตกลงเดียวกันทั่วทั้งโปรเจกต์ ทำให้การเปลี่ยน config ตรวจสอบใน diff ได้ง่ายขึ้น
TOML v1.0.0 specification ที่เสร็จสมบูรณ์ในเดือนมกราคม 2021 กำหนด grammar อย่างเข้มงวดพอที่ parser ที่สอดคล้องใดก็ตามจะสร้างโครงสร้างข้อมูลที่เหมือนกันจาก input เดียวกัน การจัดรูปแบบไม่เปลี่ยนเนื้อหาเชิงความหมายของไฟล์ TOML มันปรับเพียง whitespace การจัดกลุ่ม key และรูปแบบ quoting เท่านั้น ซึ่งหมายความว่าคุณสามารถจัดรูปแบบไฟล์ TOML ได้อย่างอิสระโดยไม่ต้องกังวลว่าจะทำให้พฤติกรรมของแอปพลิเคชันพัง
ต่างจาก JSON ตรงที่ TOML รองรับ comment ประเภทวันที่-เวลาในตัว และรูปแบบ string หลายแบบ (basic, literal และ multi-line) ตัวจัดรูปแบบที่ดีจะเก็บรักษา comment ไว้และรักษาความแตกต่างระหว่าง inline table กับ standard table header นอกจากนี้ยังจัดการ array of table ได้อย่างถูกต้อง โดยรักษาการจัดกลุ่ม section ให้ครบถ้วนเพื่อให้ไฟล์อ่านได้ง่ายสำหรับทั้งมนุษย์และ parser ที่ใช้งาน
title="My App" version="1.0.0" debug=false [database] host="localhost" port=5432 name="mydb" [database.pool] max_connections=25 timeout=30 [[servers]] name="web" host="web.example.com" [[servers]] name="api" host="api.example.com"
title = "My App" version = "1.0.0" debug = false [database] host = "localhost" port = 5432 name = "mydb" [database.pool] max_connections = 25 timeout = 30 [[servers]] name = "web" host = "web.example.com" [[servers]] name = "api" host = "api.example.com"
ทำไมต้องใช้ตัวจัดรูปแบบ TOML?
ไฟล์ config สะสมความแตกต่างด้านรูปแบบเมื่อสมาชิกทีมหลายคนแก้ไขตามเวลา tab ปะปนกับ space บาง key ถูก quote โดยไม่จำเป็น และ table section สูญเสียการจัดกลุ่มทางภาพ ตัวจัดรูปแบบ TOML ทำให้ทุกอย่างเป็นมาตรฐานในครั้งเดียว
กรณีการใช้งานตัวจัดรูปแบบ TOML
ตารางอ้างอิงไวยากรณ์ TOML
TOML มีชุด construct ขนาดเล็ก ตารางด้านล่างแสดง element เชิงโครงสร้างทุกตัวที่กำหนดใน TOML v1.0.0 specification ตัวจัดรูปแบบใช้ spacing และ grouping ที่สม่ำเสมอกับทั้งหมดนี้
| ไวยากรณ์ | ชื่อ | หมายเหตุ |
|---|---|---|
| key = "value" | Basic key-value pair | Keys are bare or quoted; values are typed |
| [table] | Standard table | Creates a named section (hash table) |
| [a.b.c] | Dotted table | Shorthand for nested tables |
| [[array]] | Array of tables | Each [[name]] block appends to an array |
| key = """...\n""" | Multi-line basic string | Allows newlines, escapes processed |
| key = '''...\n''' | Multi-line literal string | Allows newlines, no escape processing |
| # comment | Comment | Extends to end of line; not in JSON output |
| {inline = true} | Inline table | Single-line table, no newlines allowed |
TOML vs JSON vs YAML
TOML, JSON และ YAML แก้ปัญหาที่ซ้อนทับกัน แต่ตัดสินใจแลกเปลี่ยนที่แตกต่างกัน
| คุณสมบัติ | TOML | JSON | YAML |
|---|---|---|---|
| Comment | # comment บรรทัด | ไม่รองรับ | # comment บรรทัด |
| Value ที่มีประเภท | String, int, float, bool, datetime | String, number, bool, null | อนุมาน (เกิดข้อผิดพลาดได้ง่าย) |
| การซ้อน | Header [table] | วงเล็บปีกกา | ใช้ indentation |
| ความเข้มงวดของ spec | เข้มงวด (ผลลัพธ์การ parse เดียว) | เข้มงวด (RFC 8259) | ผ่อนปรน (parse ได้หลายแบบ) |
| รองรับวันที่/เวลา | 4 ประเภทในตัว | ไม่มี (ใช้ string แทน) | อนุมาน (ไม่แน่นอน) |
| Trailing comma | ไม่อนุญาต | ไม่อนุญาต | ไม่มี (ไม่ใช้ comma) |
ตัวอย่าง Code
ตัวอย่างด้านล่างจัดรูปแบบ TOML ด้วยโปรแกรมในหลายภาษาและเครื่องมือ แต่ละตัวอย่างอ่านไฟล์ parse แล้วเขียนผลลัพธ์ที่จัดรูปแบบแล้ว
import { parse, stringify } from '@iarna/toml'
import fs from 'fs'
const raw = fs.readFileSync('config.toml', 'utf-8')
const doc = parse(raw)
const formatted = stringify(doc)
// stringify() outputs canonical TOML with consistent spacing
fs.writeFileSync('config.toml', formatted)
// Quick one-liner with npx:
// npx taplo fmt config.tomlimport tomllib # Python 3.11+ (read-only)
import tomli_w # pip install tomli-w (write)
# Parse and re-serialize to format
with open("config.toml", "rb") as f:
data = tomllib.load(f)
formatted = tomli_w.dumps(data)
# tomli_w produces sorted keys, consistent quoting, and
# proper whitespace around = signs
print(formatted)
# CLI alternative: taplo fmt config.tomlpackage main
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
"bytes"
)
func main() {
var data map[string]interface{}
_, err := toml.DecodeFile("config.toml", &data)
if err != nil {
fmt.Fprintln(os.Stderr, err) // parse error with line number
os.Exit(1)
}
var buf bytes.Buffer
enc := toml.NewEncoder(&buf)
enc.Indent = " "
enc.Encode(data) // re-serialized with consistent formatting
fmt.Print(buf.String())
}# Install taplo — the standard TOML toolkit cargo install taplo-cli # or: npm install -g @taplo/cli # Format a single file in place taplo fmt config.toml # Format all .toml files in a project taplo fmt # Check formatting without modifying (CI-friendly) taplo fmt --check # Validate TOML syntax without formatting taplo lint config.toml