# Referensi Cepat YAML

*Scalar, sequence, mapping, anchor, multi-baris*

> Source: YAML Specification (yaml.org) · MIT

## Sintaks

### Aturan Dasar

| Command | Description |
|---------|-------------|
| `Indentation` | Hanya spasi (tanpa tab), kedalaman konsisten |
| `#` | Komentar (sampai akhir baris) |
| `---` | Penanda awal dokumen |
| `...` | Penanda akhir dokumen |
| `Case sensitive` | Kunci dan nilai membedakan huruf besar-kecil |

### Contoh Minimal

```
---
name: Alice
age: 30
active: true
```

## Scalar

### Tipe Scalar

| Command | Description |
|---------|-------------|
| `hello` | String (tanpa kutipan) |
| `"hello"` | String (kutip ganda, mendukung escape) |
| `'hello'` | String (kutip tunggal, literal) |
| `42 / 3.14` | Angka (integer / float) |
| `true / false` | Boolean |
| `null / ~` | Nilai null |
| `2026-03-26` | Tanggal (ISO 8601) |

### Aturan Pengutipan

```
plain: tidak perlu tanda kutip
special: "titik dua: dan tanda pagar # perlu dikutip"
escape: "line\nnewline"
literal: 'tidak ada \n escape di sini'
```

## Sequence

### Sequence Blok

```
fruits:
  - apple
  - banana
  - cherry
```

### Sequence Flow (Inline)

```
colors: [red, green, blue]
matrix: [[1, 2], [3, 4]]
```

### Sequence Objek

```
users:
  - name: Alice
    role: admin
  - name: Bob
    role: user
```

## Mapping

### Mapping Blok

```
server:
  host: localhost
  port: 8080
  debug: false
```

### Mapping Flow (Inline)

```
point: {x: 10, y: 20}
```

### Kunci Kompleks

```
? [first, second]
: "nilai kunci majemuk"
```

## Anchor dan Alias

### Definisi dan Penggunaan Ulang

```
defaults: &defaults
  timeout: 30
  retries: 3
production:
  <<: *defaults
  timeout: 60
```

### Referensi

| Command | Description |
|---------|-------------|
| `&name` | Definisikan anchor |
| `*name` | Referensi (alias) ke anchor |
| `<<` | Kunci merge — gabungkan mapping ke saat ini |

## String Multi-baris

### Scalar Blok

| Command | Description |
|---------|-------------|
| `\| (literal)` | Pertahankan baris baru persis |
| `> (folded)` | Lipat baris baru menjadi spasi |
| `\|+ (keep)` | Literal, pertahankan baris baru trailing |
| `\|- (strip)` | Literal, hapus baris baru trailing |
| `>- (fold+strip)` | Dilipat, hapus baris baru trailing |

### Blok Literal

```
script: |
  echo "line one"
  echo "line two"
```

### Blok Folded

```
description: >
  Teks panjang ini akan
  dilipat menjadi satu baris
  dengan spasi.
```

## Tag

### Tag Tipe

| Command | Description |
|---------|-------------|
| `!!str 42` | Paksa nilai menjadi string "42" |
| `!!int "42"` | Paksa nilai menjadi integer 42 |
| `!!float 1` | Paksa nilai menjadi float 1.0 |
| `!!bool "true"` | Paksa nilai menjadi boolean |
| `!!null ""` | Paksa nilai menjadi null |
| `!!binary` | Data biner yang di-encode base64 |

### Contoh Tag

```
port: !!str 8080
enabled: !!bool "yes"
version: !!float 3
```

## Pola Umum

### Docker Compose

```
services:
  web:
    image: nginx:alpine
    ports: ["80:80"]
    environment:
      NODE_ENV: production
```

### GitHub Actions

```
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
```

### Tips

| Command | Description |
|---------|-------------|
| `yamllint` | Lint YAML untuk sintaks dan gaya |
| `yq` | Pemroses YAML command-line (seperti jq) |
| `Avoid Norway` | NO adalah boolean false — beri kutipan pada kode negara |
| `Avoid !!python` | Jangan pernah muat YAML tidak tepercaya dengan loader tidak aman |
