YAML

jaytiger·2022년 1월 2일
0

☞ 참고 사이트

YAML

YAML (a recursive acronym for “YAML Ain’t Markup Language”) is a data serialization language designed to be human-friendly and work well with modern programming languages for common everyday tasks.

  • XML, JSON 과 비교
  • 데이터 전송/교환을 위한 형식 표현 언어
  • Doker, K8S,

Style

  • block style - indentation 이용
  • flow style - indicator 사용

Collections

YAML’s block collections use indentation for scope.

block style로 collections을 나타낼 때에는 들여쓰기를 사용한다.

# Example 2.1. Sequence of Scalars (ball players)
- Mark McGwire
- Sammy Sosa
- Ken Griffey


#Example 2.2. Mapping Scalars to Scalars (player statistics)
hr:  65    # Home runs
avg: 0.278 # Batting average
rbi: 147   # Runs Batted In

#Example 2.3. Mapping Scalars to Sequences (ball clubs in each league)
american:
  - Boston Red Sox
  - Detroit Tigers
  - New York Yankees
national:
  - New York Mets
  - Chicago Cubs
  - Atlanta Braves
  
#Example 2.4. Sequence of Mappings (players’ statistics)
- 
  name: Mark McGwire
  hr:   65
  avg:  0.278
- 
  name: Sammy Sosa
  hr:   63
  avg:  0.288  

flow style에서 sequence는 [], mapping은 {} 를 사용하여 표현한다.

#Example 2.5. Sequence of Sequences

- [name        , hr, avg  ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa  , 63, 0.288]


#Example 2.6 Mapping of Mappings

Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
  hr: 63,
  avg: 0.288,
}

Structures

YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present. Three dots ( “...”) indicate the end of a document without starting a new one, for use in communication channels.

YAML을 Data Serialization Language로 정의하고 있으니 이 관점에서 위의 문장을 이해하면 될 듯 하다.

*directives : 현재 YAML, TAG 두가지 지시문이 정의되어 있다.

A question mark and space (“? ”) indicate a complex mapping key. Within a block collection, key/value pairs can start immediately following the dash, colon or question mark.

키값이 scalar가 아닌 complex type의 키값일 경우 ? 사용

#Example 2.11 Mapping between Sequences

? - Detroit Tigers
  - Chicago cubs
: - 2001-07-23

? [ New York Yankees,
    Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
    2001-08-14 ]

   
#Example 2.12 Compact Nested Mapping
---
# Products purchased
- item    : Super Hoop
  quantity: 1
- item    : Basketball
  quantity: 4
- item    : Big Shoes
  quantity: 1

Scalars

Scalar content can be written in block notation, using a literal style (indicated by “|”) where all line breaks are significant. Alternatively, they can be written with the folded style (denoted by “>”) where each line break is folded to a space unless it ends an empty or a more-indented line.

  • block notation : 줄바꿈을 어떻게 다루는지에 따라 ...
    • literal style |, |-, |+` (block Chomping Indicator)
    • folded style >
#Example 2.13 In literals, newlines are preserved
# ASCII Art
--- |
  \//||\/||
  // ||  ||__

#Example 2.14 In the folded scalars, newlines become spaces
--- >
  Mark McGwire's
  year was crippled
  by a knee injury.

#Example 2.15 Folded newlines are preserved for “more indented” and blank lines
--- >
 Sammy Sosa completed another
 fine season with great stats.

   63 Home Runs
   0.288 Batting Average

 What a year!
 
#Example 2.16 Indentation determines scope
name: Mark McGwire
accomplishment: >
  Mark set a major league
  home run record in 1998.
stats: |
  65 Home Runs
  0.278 Batting Average
  • flow sclars

YAML’s flow scalars include the plain style (most examples thus far) and two quoted styles. The double-quoted style provides escape sequences. The single-quoted style is useful when escaping is not needed. All flow scalars can span multiple lines; line breaks are always folded.

#Example 2.17 Quoted Scalars

unicode: "Sosa did fine.\u263A"
control: "\b1998\t1999\t2000\n"
hex esc: "\x0d\x0a is \r\n"

single: '"Howdy!" he cried.'
quoted: ' # Not a ''comment''.'
tie-fighter: '|\-*-/|'


#Example 2.18 Multi-line Flow Scalars

plain:
  This unquoted scalar
  spans many lines.

quoted: "So does this
  quoted scalar.\n"

Tags

Explicit typing is denoted with a tag using the exclamation point (“!”) symbol.

Example 2.23 Various Explicit Tags

---
not-date: !!str 2002-04-28

picture: !!binary |
 R0lGODlhDAAMAIQAAP//9/X
 17unp5WZmZgAAAOfn515eXv
 Pz7Y6OjuDg4J+fn5OTk6enp
 56enmleECcgggoBADs=

application specific tag: !something |
 The semantics of the tag
 above may be different for
 different documents.
 
 
Example 2.24 Global Tags

%TAG ! tag:clarkevans.com,2002:
--- !shape
  # Use the ! handle for presenting
  # tag:clarkevans.com,2002:circle
- !circle
  center: &ORIGIN {x: 73, y: 129}
  radius: 7
- !line
  start: *ORIGIN
  finish: { x: 89, y: 102 }
- !label
  start: *ORIGIN
  color: 0xFFEEBB
  text: Pretty vector drawing.
  
  
Example 2.25 Unordered Sets

# Sets are represented as a
# Mapping where each key is
# associated with a null value
--- !!set
? Mark McGwire
? Sammy Sosa
? Ken Griffey


Example 2.26 Ordered Mappings

# Ordered maps are represented as
# A sequence of mappings, with
# each mapping having one key
--- !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffey: 58

Anchors

In the representation graph, a node may appear in more than one collection. When serializing such data, the first occurrence of the node is identified by an anchor. Each subsequent occurrence is serialized as an alias node which refers back to this anchor.

Merge

---
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }

# All the following maps are equal:

- # Explicit keys
  x: 1
  y: 2
  r: 10
  label: center/big

- # Merge one map
  << : *CENTER
  r: 10
  label: center/big

- # Merge multiple maps
  << : [ *CENTER, *BIG ]
  label: center/big

- # Override
  << : [ *BIG, *LEFT, *SMALL ]
  x: 1
  label: center/big
profile
Jaytiger

0개의 댓글