본문 바로가기
카테고리 없음

YAML (프로그래밍 언어)

by yororing 2024. 4. 26.

01 YAML이란

  • 'Yet Another Markup Language'의 약자
  • '사람이 읽을 수 있는' 데이터 직렬화 언어
  • 구성 파일 (Configuration File)을 작성할 때 자주 사용되는 데이터 직렬화 언어 (data serialization language)
  • xml 파일, json 파일도 사람이 읽을 수 있는데 yaml 파일은 어떻게 다른지 궁금하다면..!

  • 위와 같이 yaml 파일은 설정에서 필요한 spec과 property 값이 한 눈에 들어옴
  • 파일 작성도 다른 양식에 비해 매우 편리
  • 그러므로 많이 애용

02 YAML 문법

  • key: value 구성으로 작
  • extension: .yml or .yaml 
  • has features that come from Perl, C, XML, HTML, and other programming languages
  • JSON 파일과 상위 호환 가능 → Json 시퀀스 및 맵 사용 가
  • 3 dashes (---) 로 시작, 3 dots (...) 로 끝맺음
  • There are no usual format symbols (i.e., braces, square brackets, closing tags, or 물음표) 
  • Python-style indentation 사용
  • Tab 미사용, 공백 사용
  • # 로 주석처리 - does not support multi-line comment, each line needs to be suffixed with #
  • 예시
#Comment: Student record
#Describes some characteristics and preferences
---
name: Martin D'vloper #key-value
age: 26
hobbies: 
  - painting #first list item
  - playing_music #second list item
  - cooking #third list item
programming_languages:
  java: Intermediate
  python: Advanced
  javascript: Beginner
favorite_food: 
  - vegetables: tomatoes 
  - fruits: 
      citrics: oranges 
      tropical: bananas
      nuts: peanuts
      sweets: raisins

 

  • PyYAML 라이브러리를 통해 위의 YAML 파일을 Python으로 번역 시 다음과 같은 데이터 구조를 지님:
[
    {
        "name": "Martin D'vloper",
        "age": 26,
        "hobbies": ["painting", "playing_music", "cooking"],
        "programming_languages": {
            "java": "Intermediate",
            "python": "Advanced",
            "javascript": "Beginner",
        },
        "favorite_food": [
            {"vegetables": "tomatoes"},
            {
                "fruits": {
                    "citrics": "oranges",
                    "tropical": "bananas",
                    "nuts": "peanuts",
                    "sweets": "raisins",
                }
            },
        ],
    }
]

03 YAML 구조

  • a map or a list
  • follows a hierarchy depending on the indentation and how you define your key values.
  • Maps allow you to associate key-value pairs.
  • Each key must be unique, 순서 상관 없음

1) map

  • A map in YAML needs to be resolved before it can be closed, and a new map is created. 
  • A new map can be created by either increasing the indentation level or by resolving the previous map and starting an adjacent map.

2) list

  • A list includes values listed in a specific order and may contain any number of items needed.
  • A list sequence starts with a dash (-) and a space, while indentation separates it from the parent.
  • You can think of a sequence as a Python list or an array in Bash or Perl. A list can be embedded into a map.

04 YAML 파일 생성

  • When creating a YAML file, you’ll need to ensure that you follow these syntax rules and that your file is valid.
  • To achieve it, you can use a linter—an application that verifies the syntax of a file.
  • The yamllint command can help to ensure you’ve created a valid YAML file before you hand it over to an application.

참조

  1. https://www.redhat.com/en/topics/automation/what-is-yaml 
  2. https://en.wikipedia.org/wiki/YAML
  3. velog.io/@bloomspes/yaml-파일-작성-요령-기초편-스프링
  4.