/
ymlr.ex
155 lines (111 loc) · 5.04 KB
/
ymlr.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
defmodule Ymlr do
@moduledoc """
Encodes data into YAML documents using the `Ymlr.Encoder` protocol.
Every document starts with a separator ("---") and can be enhanced with comments.
"""
alias Ymlr.Encode
alias Ymlr.Encoder
@type document :: term() | {binary(), term()} | {[binary()], term()}
@doc ~S"""
Encodes a given data as YAML document with a separator ("---") at the beginning. Raises if it cannot be encoded.
Optinally you can pass a tuple with comment(s) and data as first argument.
## Options
* `atoms` - when set to `true`, encodes atom map keys with a leading colon.
* `sort_maps` - when set to `true`, sorts map by key-values.
## Examples
iex> Ymlr.document!(%{a: 1})
"---\na: 1\n"
iex> Ymlr.document!(%{a: 1}, atoms: true)
"---\n:a: 1\n"
iex> Ymlr.document!({"comment", %{a: 1}})
"---\n# comment\na: 1\n"
iex> Ymlr.document!({["comment 1", "comment 2"], %{a: 1}})
"---\n# comment 1\n# comment 2\na: 1\n"
iex> Ymlr.document!(Map.new(1..33, &{&1, &1}), sort_maps: true)
"---\n1: 1\n2: 2\n3: 3\n4: 4\n5: 5\n6: 6\n7: 7\n8: 8\n9: 9\n10: 10\n11: 11\n12: 12\n13: 13\n14: 14\n15: 15\n16: 16\n17: 17\n18: 18\n19: 19\n20: 20\n21: 21\n22: 22\n23: 23\n24: 24\n25: 25\n26: 26\n27: 27\n28: 28\n29: 29\n30: 30\n31: 31\n32: 32\n33: 33\n"
"""
@spec document!(document, opts :: Keyword.t()) :: binary()
def document!(document, opts \\ [])
def document!({lines, data}, opts) when is_list(lines) do
comments = Enum.map_join(lines, "", &"# #{&1}\n")
"---\n" <> comments <> Encode.to_s!(data, opts) <> "\n"
end
def document!({comment, data}, opts), do: document!({[comment], data}, opts)
def document!(data, opts) do
document!({[], data}, opts)
end
@doc ~S"""
Encodes a given data as YAML document with a separator ("---") at the beginning.
Optinally you can pass a tuple with comment(s) and data as first argument.
## Options
* `atoms` - when set to `true`, encodes atom map keys with a leading colon.
* `sort_maps` - when set to `true`, sorts map by key-values.
## Examples
iex> Ymlr.document(%{a: 1})
{:ok, "---\na: 1\n"}
iex> Ymlr.document(%{a: 1}, atoms: true)
{:ok, "---\n:a: 1\n"}
iex> Ymlr.document({"comment", %{a: 1}})
{:ok, "---\n# comment\na: 1\n"}
iex> Ymlr.document({["comment 1", "comment 2"], %{a: 1}})
{:ok, "---\n# comment 1\n# comment 2\na: 1\n"}
iex> Ymlr.document(Map.new(1..33, &{&1, &1}), sort_maps: true)
{:ok, "---\n1: 1\n2: 2\n3: 3\n4: 4\n5: 5\n6: 6\n7: 7\n8: 8\n9: 9\n10: 10\n11: 11\n12: 12\n13: 13\n14: 14\n15: 15\n16: 16\n17: 17\n18: 18\n19: 19\n20: 20\n21: 21\n22: 22\n23: 23\n24: 24\n25: 25\n26: 26\n27: 27\n28: 28\n29: 29\n30: 30\n31: 31\n32: 32\n33: 33\n"}
"""
@spec document(document, opts :: Encoder.opts()) :: {:ok, binary()} | {:error, binary()}
def document(document, opts \\ []) do
yml = document!(document, opts)
{:ok, yml}
rescue
e in Protocol.UndefinedError -> {:error, Exception.message(e)}
end
@doc ~S"""
Encodes a given list of data as "---" separated YAML documents. Raises if it cannot be encoded.
## Options
* `atoms` - when set to `true`, encodes atom map keys with a leading colon.
* `sort_maps` - when set to `true`, sorts map by key-values.
## Examples
iex> Ymlr.documents!([%{a: 1}])
"---\na: 1\n"
iex> Ymlr.documents!([%{a: 1}], atoms: true)
"---\n:a: 1\n"
iex> Ymlr.documents!([%{a: 1}, %{b: 2}])
"---\na: 1\n\n---\nb: 2\n"
iex> Ymlr.documents!(%{a: "a"})
** (ArgumentError) The given argument is not a list of documents. Use document/1, document/2, document!/1 or document!/2 for a single document.
"""
def documents!(documents, opts \\ [])
def documents!(documents, opts) when is_list(documents),
do: Enum.map_join(documents, "\n", &document!(&1, opts))
def documents!(_documents, _opts),
do:
raise(
ArgumentError,
"The given argument is not a list of documents. Use document/1, document/2, document!/1 or document!/2 for a single document."
)
@doc ~S"""
Encodes a given list of data as "---" separated YAML documents.
## Options
* `atoms` - when set to `true`, encodes atom map keys with a leading colon.
* `sort_maps` - when set to `true`, sorts map by key-values.
## Examples
iex> Ymlr.documents([%{a: 1}])
{:ok, "---\na: 1\n"}
iex> Ymlr.documents([%{a: 1}], atoms: true)
{:ok, "---\n:a: 1\n"}
iex> Ymlr.documents([%{a: 1}, %{b: 2}])
{:ok, "---\na: 1\n\n---\nb: 2\n"}
iex> Ymlr.documents(%{a: "a"})
{:error, "The given argument is not a list of documents. Use document/1, document/2, document!/1 or document!/2 for a single document."}
"""
@spec documents([document], opts :: Encoder.opts()) :: {:ok, binary()} | {:error, binary()}
def documents(documents, opts \\ []) do
yml = documents!(documents, opts)
{:ok, yml}
rescue
e in Protocol.UndefinedError ->
{:error, Exception.message(e)}
e in ArgumentError ->
{:error, Exception.message(e)}
end
end