DEV Community

Cover image for YAML
Enes Gerem
Enes Gerem

Posted on • Updated on

YAML

YAML yazılım dünyasında artık her yerde karşımıza çıkan bir dosya formatı ve configürasyon dosyası alternatifi haline geldi. Fakat nedir bu YAML?

YAML (YAML Ain't Markup Language) dil bağımsız bir veri değişim formatıdır. Herhangi bir programlama dili ile oluşturulan nesneler, YAML formatında kolaylıkla temsil edilebilmektedir. YAML geniş veri tipi desteği sayesinde tüm diller arasında ortak olarak kullanılabilir olmakla birlikte, ayrıca insan gözüyle bakıldığında kolay okunabilir bir yapısı mevcuttur.

YAML, konfigürasyon dosyaları için ve verilerin depolandığı veya iletildiği uygulamalarda yaygın olarak kullanılır. XML(Extensible Markup Language)'in kullanıldığı uygulamaların çoğunu hedefler ancak SGML(Standard Generalized Markup Language)'den kasıtlı olarak farklı olan minimal bir sözdizimine sahiptir.

YAML Syntaxı

YAML Nested yapısını belirtmek için Python tarzı girintiyi, listeler için köşeli parantezleri [...] ve mapler için süslü
parantezleri {...} barındıran daha kompakt bir biçim kullanır.

Bazı basit kurallar;

  • Yapıyı belirtmek için boşluk girintisi kullanılır; ancak, bu girintinin parçası olarak sekme karakterlerine izin verilmez.
  • Yorumlar "#" ile başlar, bir satırda herhangi bir yerden başlayabilir ve satırın sonuna kadar devam edebilir.
# Bu bir yorum satırıdır.
Enter fullscreen mode Exit fullscreen mode
  • Liste elemanları, her satırda bir eleman olacak şekilde "-" ile veya köşeli parantezlerin içine elemanları virgülle ayırarak gösterilir.
# Listelerin tanımlanma şekilleri
- A
- B
- C

  [ A,B,C ]
Enter fullscreen mode Exit fullscreen mode
  • Dictionary yapısı "key:value" formunda her satırda bir eleman olacak şekilde listelenebilir.
# Dictionary
enes:
  name: Enes Gerem
  age: 23
Enter fullscreen mode Exit fullscreen mode
  • Dictionary listesi aşağıdaki gibi belirtilebilir.
# Dictionary listesi
- enes:
  name: Enes Gerem
  age: 23

- emre:
  name: Emre Basar
  age: 22
Enter fullscreen mode Exit fullscreen mode
  • Alternatif olarak dictionary yapısı süslü parantezlerle de belirtilebilir.

  • "|" ve ">" kullanarak, tanımlar birden çok satırda belirtilebilir. Satır başı karakterini de almak istediğimiz de "|",
    tek bir satır yazacaksak ve sadece okunabilirliği arttırmak istiyorsak ">" kullanabiliriz.

include_newlines: |
  bu String tam olarak
  bu sekilde
  gozukecektir

fold_newlines: >
  bu değer aslinda
  sadece bir satiri
  ifade eder
Enter fullscreen mode Exit fullscreen mode
  • İki nokta'dan ":" sonra bir boşluk varsa bu bir dictionaryi temsil eder. Eğer iki noktayı value kısmında kullanmak istiyorsak boşluk olmadan, escape karakteriyle ya da tırnak içinde kullanmamız gerekir.

  • Değişkenler "{var}" formatında belirtilebilir.

foo: "{variable}"
Enter fullscreen mode Exit fullscreen mode

Aşağıda YAML formatında hazırlanmış YAML formatı için derlenen referans dosyası incelenebilir.
Kaynak: yaml.org

%YAML 1.1   # Reference card
---
Collection indicators:
  '? ': Key indicator.
  ': ': Value indicator.
  '- ': Nested series entry indicator.
  ', ': Separate in-line branch entries.
  '[]': Surround in-line series branch.
  '{}': Surround in-line keyed branch.
Scalar indicators:
  '''': Surround in-line unescaped scalar ('' escaped ').
  '"': Surround in-line escaped scalar (see escape codes below).
  '|': Block scalar indicator.
  '>': Folded scalar indicator.
  '-': Strip chomp modifier ('|-' or '>-').
  '+': Keep chomp modifier ('|+' or '>+').
  1-9: Explicit indentation modifier ('|1' or '>2').
  # Modifiers can be combined ('|2-', '>+1').
Alias indicators:
  '&': Anchor property.
  '*': Alias indicator.
Tag property: # Usually unspecified.
  none: Unspecified tag (automatically resolved by application).
  '!': Non-specific tag (by default, "!!map"/"!!seq"/"!!str").
  '!foo': Primary (by convention, means a local "!foo" tag).
  '!!foo': Secondary (by convention, means "tag:yaml.org,2002:foo").
  '!h!foo': Requires "%TAG !h! <prefix>" (and then means "<prefix>foo").
  '!<foo>': Verbatim tag (always means "foo").
Document indicators:
  '%': Directive indicator.
  '---': Document header.
  '...': Document terminator.
Misc indicators:
  ' #': Throwaway comment indicator.
  '`@': Both reserved for future use.
Special keys:
  '=': Default "value" mapping key.
  '<<': Merge keys from another mapping.
Core types: # Default automatic tags.
  '!!map': { Hash table, dictionary, mapping }
  '!!seq': { List, array, tuple, vector, sequence }
  '!!str': Unicode string
More types:
  '!!set': { cherries, plums, apples }
  '!!omap': [ one: 1, two: 2 ]
Language Independent Scalar types:
  { ~, null }: Null (no value).
    [ 1234, 0x4D2, 02333 ]: [ Decimal int, Hexadecimal int, Octal int ]
    [ 1_230.15, 12.3015e+02 ]: [ Fixed float, Exponential float ]
    [ .inf, -.Inf, .NAN ]: [ Infinity (float), Negative, Not a number ]
    { Y, true, Yes, ON }: Boolean true
    { n, FALSE, No, off }: Boolean false
    ? !!binary >
      R0lG...BADS=
      : >-
      Base 64 binary value.
Escape codes:
  Numeric: { "\x12": 8-bit, "\u1234": 16-bit, "\U00102030": 32-bit }
  Protective: { "\\": '\', "\"": '"', "\ ": ' ', "\<TAB>": TAB }
  C: { "\0": NUL, "\a": BEL, "\b": BS, "\f": FF, "\n": LF, "\r": CR,
       "\t": TAB, "\v": VTAB }
  Additional: { "\e": ESC, "\_": NBSP, "\N": NEL, "\L": LS, "\P": PS }
...

Enter fullscreen mode Exit fullscreen mode

YAML artık birçok framework'ün tercih ettiği configuration formatı haline geldi. En popüler front-end Web framework'ü
React'te ve mobil framework'ü Flutter'da bu yapıyı görmek mümkün.

Örnek olması açısından aşağıda Flutter projelerimden birinin "pubspec.yaml" configuration dosyası incelenebilir.

name: receipt_scanner
description: Receipt scanner app

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  firebase_ml_vision:
  image_picker:
  image_cropper:
  path_provider: ^1.6.11
  mailer: ^3.0.4
  excel: ^1.1.5
  camera_camera: ^1.0.34+1
  firebase_auth: ^0.16.1
  cloud_firestore: ^0.13.7
  provider: ^3.1.0
  flutter_spinkit: ^4.1.2+1
  http: ^0.12.0+3
  sqflite: ^1.3.1
  google_sign_in: ^4.5.1
  camera: ^0.5.8+5
  path: ^1.6.4
  auto_size_text: ^2.1.0
  flushbar: ^1.5.0
  rxdart: ^0.24.1


dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_launcher_icons: "^0.7.3"


flutter_icons:
  android: true
  ios: true
  image_path: "assets/icon/voucher_logo.png"


# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  assets:
  - assets/image/google.png
  - assets/image/bg.png
  - assets/files/example.xlsx
  - assets/files/temp.xlsx
  - assets/background/top.png
  - assets/background/right.png
  - assets/background/bttm.png
  - assets/icon/google.png
  - assets/icon/facebook.png
  - assets/icon/apple.png
  - assets/icon/twitter.png
  - assets/icon/receipt_logo.png
  - assets/icon/splash_screen.png
  - assets/icon/profile.png
  - assets/icon/data.png
  - assets/icon/home.png
  - assets/icon/homeon.png
  - assets/icon/dataon.png
  - assets/icon/profileon.png
  - assets/icon/user.png
  - assets/icon/lock.png

  fonts:
    - family: Poppins
      fonts:
        - asset: fonts/Poppins/Poppins-Black.ttf
          weight: 900
        - asset: fonts/Poppins/Poppins-Bold.ttf
          weight: 700
        - asset: fonts/Poppins/Poppins-ExtraBold.ttf
          weight: 800
        - asset: fonts/Poppins/Poppins-Light.ttf
          weight: 400
        - asset: fonts/Poppins/Poppins-Medium.ttf
          weight: 600
        - asset: fonts/Poppins/Poppins-Regular.ttf
          weight: 500
        - asset: fonts/Poppins/Poppins-SemiBold.ttf
          weight: 700

    - family: Spartan
      fonts:
          - asset: fonts/Spartan-VariableFont_wght.ttf 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)