-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for yaml parsing/rendering. (#2855)
- Loading branch information
Showing
16 changed files
with
282 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## Importing/exporting YAML values | ||
|
||
Support for YAML parsing and rendering was first added in liquidsoap `2.2.0`. This support follows the same pattern as [JSON parsing/rendering](json.html) but using | ||
yaml-based syntax, i.e.: | ||
|
||
```liauidsoap | ||
let yaml.parse ({ | ||
name, | ||
version, | ||
scripts, | ||
} : { | ||
name: string, | ||
version: string, | ||
scripts: { | ||
test: string? | ||
}? | ||
}) = file.contents("/path/to/file.yaml") | ||
``` | ||
|
||
and | ||
|
||
```liquidsoap | ||
r = {artist = "Bla", title = "Blo"} | ||
print(yaml.stringify(r)) | ||
``` | ||
|
||
The only major difference being that, in YAML, all numbers are parsed and rendered as _floats_. | ||
|
||
Please refer to the [JSON parsing and rendering](json.html) documentation for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ | |
tsdl-image | ||
tsdl-ttf | ||
vorbis | ||
yaml | ||
xmlplaylist) | ||
(conflicts | ||
(alsa (< 0.3.0)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,7 @@ depopts: [ | |
"tsdl-image" | ||
"tsdl-ttf" | ||
"vorbis" | ||
"yaml" | ||
"xmlplaylist" | ||
] | ||
conflicts: [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
noop.disabled.ml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
noop.enabled.ml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
(***************************************************************************** | ||
Liquidsoap, a programmable audio stream generator. | ||
Copyright 2003-2023 Savonet team | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details, fully stated in the COPYING | ||
file at the root of the liquidsoap distribution. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*****************************************************************************) | ||
|
||
let () = Atomic.set Liquidsoap_lang.Builtins_yaml.yaml_parser Yaml.of_string_exn | ||
let yaml = Lang.add_module "yaml" | ||
|
||
let rec yaml_of_json = function | ||
| `Assoc l -> `O (List.map (fun (lbl, v) -> (lbl, yaml_of_json v)) l) | ||
| `Tuple l -> `A (List.map yaml_of_json l) | ||
| `String s -> `String s | ||
| `Bool b -> `Bool b | ||
| `Float f -> `Float f | ||
| `Int i -> `Float (float i) | ||
| `Null -> `Null | ||
|
||
let scalar_style pos = function | ||
| "any" -> `Any | ||
| "plain" -> `Plain | ||
| "single_quoted" -> `Single_quoted | ||
| "double_quoted" -> `Double_quoted | ||
| "literal" -> `Literal | ||
| "folded" -> `Folded | ||
| v -> | ||
Runtime_error.raise | ||
~message:(Printf.sprintf "Invalid scalar style: %s" v) | ||
~pos "yaml" | ||
|
||
let layout_style pos = function | ||
| "any" -> `Any | ||
| "block" -> `Block | ||
| "flow" -> `Flow | ||
| v -> | ||
Runtime_error.raise | ||
~message:(Printf.sprintf "Invalid layout style: %s" v) | ||
~pos "yaml" | ||
|
||
let _ = | ||
Lang.add_builtin ~base:yaml "stringify" ~category:`String | ||
~descr: | ||
"Convert a value to YAML. If the value cannot be represented as YAML \ | ||
(for instance a function), a `error.yaml` exception is raised." | ||
[ | ||
( "scalar_style", | ||
Lang.string_t, | ||
Some (Lang.string "any"), | ||
Some | ||
"Scalar style. One of: \"any\", \"plain\", \"single_quoted\", \ | ||
\"double_quoted\", \"literal\" or \"folded\"." ); | ||
( "layout_style", | ||
Lang.string_t, | ||
Some (Lang.string "any"), | ||
Some "Layout style. One of: \"any\", \"block\" or \"flow\"." ); | ||
("", Lang.univ_t (), None, None); | ||
] | ||
Lang.string_t | ||
(fun p -> | ||
let pos = Lang.pos p in | ||
let v = List.assoc "" p in | ||
let scalar_style = | ||
scalar_style pos (Lang.to_string (List.assoc "scalar_style" p)) | ||
in | ||
let layout_style = | ||
layout_style pos (Lang.to_string (List.assoc "layout_style" p)) | ||
in | ||
try | ||
let json = Liquidsoap_lang.Builtins_json.json_of_value v in | ||
Lang.string | ||
(Yaml.to_string_exn ~encoding:`Utf8 ~scalar_style ~layout_style | ||
(yaml_of_json json)) | ||
with _ -> | ||
Runtime_error.raise | ||
~message: | ||
(Printf.sprintf "Value %s cannot be represented as YAML" | ||
(Value.to_string v)) | ||
~pos "yaml") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
type yaml = | ||
[ `Null | ||
| `Bool of bool | ||
| `Float of float | ||
| `String of string | ||
| `A of yaml list | ||
| `O of (string * yaml) list ] | ||
|
||
let yaml_parser : (string -> yaml) Atomic.t = | ||
Atomic.make (fun _ -> | ||
Runtime_error.raise | ||
~message: | ||
"YAML support not enabled! Please re-compile liquidsoap with the \ | ||
`yaml` module to enable YAML parsing and rendering." | ||
~pos:[] "not_found") | ||
|
||
let rec json_of_yaml = function | ||
| `O l -> `Assoc (List.map (fun (lbl, v) -> (lbl, json_of_yaml v)) l) | ||
| `A l -> `Tuple (List.map json_of_yaml l) | ||
| `String s -> `String s | ||
| `Bool b -> `Bool b | ||
| `Float f -> `Float f | ||
| `Null -> `Null | ||
|
||
let _ = | ||
Lang.add_builtin "_internal_yaml_parser_" ~category:`String ~flags:[`Hidden] | ||
~descr:"Internal yaml parser" | ||
[ | ||
("type", Value.RuntimeType.t, None, Some "Runtime type"); | ||
("", Lang.string_t, None, None); | ||
] | ||
(Lang.univ_t ()) | ||
(fun p -> | ||
let s = Lang.to_string (List.assoc "" p) in | ||
let ty = Value.RuntimeType.of_value (List.assoc "type" p) in | ||
let scheme = Typing.generalize ~level:(-1) ty in | ||
let ty = Typing.instantiate ~level:(-1) scheme in | ||
let parser = Atomic.get yaml_parser in | ||
try | ||
let yaml = parser s in | ||
Builtins_json.value_of_typed_json ~ty (json_of_yaml yaml) | ||
with exn -> ( | ||
let bt = Printexc.get_raw_backtrace () in | ||
match exn with | ||
| _ -> | ||
Runtime_error.raise ~bt ~pos:(Lang.pos p) | ||
~message: | ||
(Printf.sprintf | ||
"Parse error: yaml value cannot be parsed as type: %s" | ||
(Type.to_string ty)) | ||
"yaml")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,7 @@ | |
builtins_profiler | ||
builtins_regexp | ||
builtins_string | ||
builtins_yaml | ||
builtins_ref | ||
console | ||
doc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.