This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new a6bd9bd092 docs: render JSON examples for map-valued options in
service snippets (#9317)
a6bd9bd092 is described below
commit a6bd9bd092012fb3bf65f886073f112cfcddcc8e
Author: Chitral Verma <[email protected]>
AuthorDate: Sat Nov 28 21:39:30 2026 +0530
docs: render JSON examples for map-valued options in service snippets
(#9317)
* docs: render JSON examples for map-valued options in service snippets
Map options take a JSON object string since #9262, so render
{"key":"value"} instead of ... and escape values inside each
binding's string literal.
Signed-off-by: Chitral Verma <[email protected]>
* docs(website): say map options take a JSON object string
Signed-off-by: Chitral Verma <[email protected]>
---------
Signed-off-by: Chitral Verma <[email protected]>
---
dev/src/generate/docs.rs | 124 +++++++++++++++++++++---
website/data/services.json | 20 ++--
website/src/components/services/ServicePage.jsx | 5 +-
3 files changed, 127 insertions(+), 22 deletions(-)
diff --git a/dev/src/generate/docs.rs b/dev/src/generate/docs.rs
index ad2ea07e0a..ae57976b70 100644
--- a/dev/src/generate/docs.rs
+++ b/dev/src/generate/docs.rs
@@ -363,10 +363,12 @@ fn friendly_type(ty: ConfigType) -> &'static str {
/// falls back to a per-type placeholder. Every binding here passes config as a
/// `String -> String` map, so even non-string types are quoted strings that
/// OpenDAL's `ConfigDeserializer` parses (e.g. `"true"`, `"10s"`, `"a,b"`).
+/// Map options take a JSON object string such as `{"key":"value"}`.
fn value_of<'a>(f: &Field<'a>) -> &'a str {
let placeholder = match f.value {
ConfigType::Bool => "true",
- ConfigType::String | ConfigType::HashMap => "...",
+ ConfigType::String => "...",
+ ConfigType::HashMap => r#"{"key":"value"}"#,
ConfigType::Duration => "10s",
ConfigType::Usize
| ConfigType::U64
@@ -378,6 +380,12 @@ fn value_of<'a>(f: &Field<'a>) -> &'a str {
f.default.or(f.example).unwrap_or(placeholder)
}
+/// Double-quoted string literal for `value`, escaping `\` and `"`. The result
+/// is valid in every binding's double-quoted string syntax.
+fn string_literal(value: &str) -> String {
+ format!("\"{}\"", value.replace('\\', "\\\\").replace('"', "\\\""))
+}
+
/// Per-binding syntax used by the generic snippet builder.
struct Syntax {
/// Import lines plus the opening of the call, ready for fields to follow.
@@ -463,9 +471,9 @@ fn render_examples(
comment: "// ",
assign: |f| {
format!(
- "(\"{}\".to_string(), \"{}\".to_string()),",
+ "(\"{}\".to_string(), {}.to_string()),",
f.name,
- value_of(f)
+ string_literal(value_of(f))
)
},
},
@@ -474,7 +482,7 @@ fn render_examples(
close: ")".to_string(),
indent: " ",
comment: "# ",
- assign: |f| format!("{}=\"{}\",", f.name, value_of(f)),
+ assign: |f| format!("{}={},", f.name, string_literal(value_of(f))),
},
"nodejs" => Syntax {
open: format!(
@@ -483,14 +491,14 @@ fn render_examples(
close: "});".to_string(),
indent: " ",
comment: "// ",
- assign: |f| format!("{}: \"{}\",", f.name, value_of(f)),
+ assign: |f| format!("{}: {},", f.name,
string_literal(value_of(f))),
},
"java" => Syntax {
open: "import java.util.HashMap;\nimport java.util.Map;\nimport
org.apache.opendal.Operator;\n\nMap<String, String> config = new
HashMap<>();\n".to_string(),
close: format!("Operator operator = Operator.of(\"{scheme}\",
config);"),
indent: "",
comment: "// ",
- assign: |f| format!("config.put(\"{}\", \"{}\");", f.name,
value_of(f)),
+ assign: |f| format!("config.put(\"{}\", {});", f.name,
string_literal(value_of(f))),
},
"go" => Syntax {
open: format!(
@@ -499,7 +507,7 @@ fn render_examples(
close: "})".to_string(),
indent: " ",
comment: "// ",
- assign: |f| format!("\"{}\": \"{}\",", f.name, value_of(f)),
+ assign: |f| format!("\"{}\": {},", f.name,
string_literal(value_of(f))),
},
"c" => Syntax {
open: "#include \"opendal.h\"\n\nopendal_operator_options *options
= opendal_operator_options_new();\n".to_string(),
@@ -510,9 +518,9 @@ fn render_examples(
comment: "// ",
assign: |f| {
format!(
- "opendal_operator_options_set(options, \"{}\", \"{}\");",
+ "opendal_operator_options_set(options, \"{}\", {});",
f.name,
- value_of(f)
+ string_literal(value_of(f))
)
},
},
@@ -521,7 +529,7 @@ fn render_examples(
close: format!("}};\nopendal::Operator operator(\"{scheme}\",
config);"),
indent: " ",
comment: "// ",
- assign: |f| format!("{{\"{}\", \"{}\"}},", f.name, value_of(f)),
+ assign: |f| format!("{{\"{}\", {}}},", f.name,
string_literal(value_of(f))),
},
"ruby" => Syntax {
open: format!(
@@ -530,7 +538,14 @@ fn render_examples(
close: "})".to_string(),
indent: " ",
comment: "# ",
- assign: |f| format!("\"{}\" => \"{}\",", f.name, value_of(f)),
+ // `\#` is a literal `#` in Ruby and prevents `#{...}`
interpolation.
+ assign: |f| {
+ format!(
+ "\"{}\" => {},",
+ f.name,
+ string_literal(value_of(f)).replace('#', "\\#")
+ )
+ },
},
other => unreachable!("no renderer for binding {other}"),
};
@@ -564,4 +579,91 @@ mod tests {
assert!(full.contains(&format!(" {assignment}")));
assert!(!full.contains(&format!("// {assignment}")));
}
+
+ fn map_field() -> Field<'static> {
+ Field {
+ name: "options",
+ value: ConfigType::HashMap,
+ required: false,
+ minimal: false,
+ comments: "Extra options.",
+ group: DEFAULT_GROUP,
+ default: None,
+ example: None,
+ }
+ }
+
+ fn render_full(binding: &str, field: &Field) -> String {
+ let grouped = vec![(DEFAULT_GROUP, vec![field])];
+ render_examples(binding, "demo", &[], &grouped).1
+ }
+
+ #[test]
+ fn map_fields_render_json_object_literals() {
+ let field = map_field();
+ let cases = [
+ (
+ "rust",
+ r#" // ("options".to_string(),
"{\"key\":\"value\"}".to_string()),"#,
+ ),
+ (
+ "java",
+ r#"// config.put("options", "{\"key\":\"value\"}");"#,
+ ),
+ ("python", r#" # options="{\"key\":\"value\"}","#),
+ ("nodejs", r#" // options: "{\"key\":\"value\"}","#),
+ ("go", "\t// \"options\": \"{\\\"key\\\":\\\"value\\\"}\","),
+ (
+ "c",
+ r#"// opendal_operator_options_set(options, "options",
"{\"key\":\"value\"}");"#,
+ ),
+ ("cpp", r#" // {"options", "{\"key\":\"value\"}"},"#),
+ ("ruby", r#" # "options" => "{\"key\":\"value\"}","#),
+ ];
+ for (binding, line) in cases {
+ let full = render_full(binding, &field);
+ assert!(
+ full.lines().any(|l| l == line),
+ "{binding}: expected line {line:?} in:\n{full}"
+ );
+ }
+ }
+
+ #[test]
+ fn rust_map_example_is_a_valid_json_object() {
+ let full = render_full("rust", &map_field());
+ let line = full
+ .lines()
+ .find(|l| l.contains("\"options\""))
+ .expect("options line");
+ let start = line.find(", ").expect("value separator") + 2;
+ let end = line.rfind(".to_string()").expect("value end");
+ let lit: syn::LitStr =
+ syn::parse_str(&line[start..end]).expect("valid Rust string
literal");
+ let value: std::collections::HashMap<String, String> =
+ serde_json::from_str(&lit.value()).expect("JSON object of
strings");
+ assert_eq!(
+ value,
+ std::collections::HashMap::from([("key".to_string(),
"value".to_string())])
+ );
+ }
+
+ #[test]
+ fn ruby_literal_escapes_interpolation() {
+ let field = Field {
+ name: "prefix",
+ value: ConfigType::String,
+ required: true,
+ minimal: false,
+ comments: "",
+ group: DEFAULT_GROUP,
+ default: Some("a#{b}"),
+ example: None,
+ };
+ let full = render_full("ruby", &field);
+ assert!(
+ full.lines().any(|l| l == r#" "prefix" => "a\#{b}","#),
+ "{full}"
+ );
+ }
}
diff --git a/website/data/services.json b/website/data/services.json
index 9cd4b6e03f..c80e1be340 100644
--- a/website/data/services.json
+++ b/website/data/services.json
@@ -3110,25 +3110,25 @@
"binding": "rust",
"language": "rust",
"minimal": "use opendal::Operator;\n\nlet operator =
Operator::via_iter(\"hdfs-native\", [\n])?;",
- "full": "use opendal::Operator;\n\nlet operator =
Operator::via_iter(\"hdfs-native\", [\n // work dir of this backend\n //
(\"root\".to_string(), \"...\".to_string()),\n\n // name_node of this
backend\n // (\"name_node\".to_string(), \"...\".to_string()),\n\n //
other options for hdfs client\n // (\"options\".to_string(),
\"...\".to_string()),\n])?;"
+ "full": "use opendal::Operator;\n\nlet operator =
Operator::via_iter(\"hdfs-native\", [\n // work dir of this backend\n //
(\"root\".to_string(), \"...\".to_string()),\n\n // name_node of this
backend\n // (\"name_node\".to_string(), \"...\".to_string()),\n\n //
other options for hdfs client\n // (\"options\".to_string(),
\"{\\\"key\\\":\\\"value\\\"}\".to_string()),\n])?;"
},
{
"binding": "python",
"language": "python",
"minimal": "import opendal\n\noperator = opendal.Operator(\n
\"hdfs-native\",\n)",
- "full": "import opendal\n\noperator = opendal.Operator(\n
\"hdfs-native\",\n # work dir of this backend\n # root=\"...\",\n\n #
name_node of this backend\n # name_node=\"...\",\n\n # other options for
hdfs client\n # options=\"...\",\n)"
+ "full": "import opendal\n\noperator = opendal.Operator(\n
\"hdfs-native\",\n # work dir of this backend\n # root=\"...\",\n\n #
name_node of this backend\n # name_node=\"...\",\n\n # other options for
hdfs client\n # options=\"{\\\"key\\\":\\\"value\\\"}\",\n)"
},
{
"binding": "nodejs",
"language": "javascript",
"minimal": "import { Operator } from \"opendal\";\n\nconst operator
= new Operator(\"hdfs-native\", {\n});",
- "full": "import { Operator } from \"opendal\";\n\nconst operator =
new Operator(\"hdfs-native\", {\n // work dir of this backend\n // root:
\"...\",\n\n // name_node of this backend\n // name_node: \"...\",\n\n //
other options for hdfs client\n // options: \"...\",\n});"
+ "full": "import { Operator } from \"opendal\";\n\nconst operator =
new Operator(\"hdfs-native\", {\n // work dir of this backend\n // root:
\"...\",\n\n // name_node of this backend\n // name_node: \"...\",\n\n //
other options for hdfs client\n // options:
\"{\\\"key\\\":\\\"value\\\"}\",\n});"
},
{
"binding": "go",
"language": "go",
"minimal": "import
(\n\t\"github.com/apache/opendal-go-services/hdfs-native\"\n\topendal
\"github.com/apache/opendal/bindings/go\"\n)\n\noperator, err :=
opendal.NewOperator(hdfs-native.Scheme, opendal.OperatorOptions{\n})",
- "full": "import
(\n\t\"github.com/apache/opendal-go-services/hdfs-native\"\n\topendal
\"github.com/apache/opendal/bindings/go\"\n)\n\noperator, err :=
opendal.NewOperator(hdfs-native.Scheme, opendal.OperatorOptions{\n\t// work dir
of this backend\n\t// \"root\": \"...\",\n\n\t// name_node of this
backend\n\t// \"name_node\": \"...\",\n\n\t// other options for hdfs
client\n\t// \"options\": \"...\",\n})"
+ "full": "import
(\n\t\"github.com/apache/opendal-go-services/hdfs-native\"\n\topendal
\"github.com/apache/opendal/bindings/go\"\n)\n\noperator, err :=
opendal.NewOperator(hdfs-native.Scheme, opendal.OperatorOptions{\n\t// work dir
of this backend\n\t// \"root\": \"...\",\n\n\t// name_node of this
backend\n\t// \"name_node\": \"...\",\n\n\t// other options for hdfs
client\n\t// \"options\": \"{\\\"key\\\":\\\"value\\\"}\",\n})"
}
]
},
@@ -6770,37 +6770,37 @@
"binding": "rust",
"language": "rust",
"minimal": "use opendal::Operator;\n\nlet operator =
Operator::via_iter(\"s3\", [\n (\"bucket\".to_string(),
\"my-bucket\".to_string()),\n])?;",
- "full": "use opendal::Operator;\n\nlet operator =
Operator::via_iter(\"s3\", [\n // --- General ---\n // root of this
backend.\n //\n // All operations will happen under this root.\n //\n
// default to `/` if not set.\n // (\"root\".to_string(),
\"/\".to_string()),\n\n // bucket name of this backend.\n //\n //
required.\n (\"bucket\".to_string(), \"my-bucket\".to_string()),\n\n //
endpoint of this backend.\n //\n // Endpoint must be ful [...]
+ "full": "use opendal::Operator;\n\nlet operator =
Operator::via_iter(\"s3\", [\n // --- General ---\n // root of this
backend.\n //\n // All operations will happen under this root.\n //\n
// default to `/` if not set.\n // (\"root\".to_string(),
\"/\".to_string()),\n\n // bucket name of this backend.\n //\n //
required.\n (\"bucket\".to_string(), \"my-bucket\".to_string()),\n\n //
endpoint of this backend.\n //\n // Endpoint must be ful [...]
},
{
"binding": "java",
"language": "java",
"minimal": "import java.util.HashMap;\nimport java.util.Map;\nimport
org.apache.opendal.Operator;\n\nMap<String, String> config = new
HashMap<>();\nconfig.put(\"bucket\", \"my-bucket\");\nOperator operator =
Operator.of(\"s3\", config);",
- "full": "import java.util.HashMap;\nimport java.util.Map;\nimport
org.apache.opendal.Operator;\n\nMap<String, String> config = new
HashMap<>();\n// --- General ---\n// root of this backend.\n//\n// All
operations will happen under this root.\n//\n// default to `/` if not set.\n//
config.put(\"root\", \"/\");\n\n// bucket name of this backend.\n//\n//
required.\nconfig.put(\"bucket\", \"my-bucket\");\n\n// endpoint of this
backend.\n//\n// Endpoint must be full uri, e.g.\n//\n// [...]
+ "full": "import java.util.HashMap;\nimport java.util.Map;\nimport
org.apache.opendal.Operator;\n\nMap<String, String> config = new
HashMap<>();\n// --- General ---\n// root of this backend.\n//\n// All
operations will happen under this root.\n//\n// default to `/` if not set.\n//
config.put(\"root\", \"/\");\n\n// bucket name of this backend.\n//\n//
required.\nconfig.put(\"bucket\", \"my-bucket\");\n\n// endpoint of this
backend.\n//\n// Endpoint must be full uri, e.g.\n//\n// [...]
},
{
"binding": "python",
"language": "python",
"minimal": "import opendal\n\noperator = opendal.Operator(\n
\"s3\",\n bucket=\"my-bucket\",\n)",
- "full": "import opendal\n\noperator = opendal.Operator(\n
\"s3\",\n # --- General ---\n # root of this backend.\n #\n # All
operations will happen under this root.\n #\n # default to `/` if not
set.\n # root=\"/\",\n\n # bucket name of this backend.\n #\n #
required.\n bucket=\"my-bucket\",\n\n # endpoint of this backend.\n
#\n # Endpoint must be full uri, e.g.\n #\n # - AWS S3:
`https://s3.amazonaws.com` or `https://s3.{region [...]
+ "full": "import opendal\n\noperator = opendal.Operator(\n
\"s3\",\n # --- General ---\n # root of this backend.\n #\n # All
operations will happen under this root.\n #\n # default to `/` if not
set.\n # root=\"/\",\n\n # bucket name of this backend.\n #\n #
required.\n bucket=\"my-bucket\",\n\n # endpoint of this backend.\n
#\n # Endpoint must be full uri, e.g.\n #\n # - AWS S3:
`https://s3.amazonaws.com` or `https://s3.{region [...]
},
{
"binding": "nodejs",
"language": "javascript",
"minimal": "import { Operator } from \"opendal\";\n\nconst operator
= new Operator(\"s3\", {\n bucket: \"my-bucket\",\n});",
- "full": "import { Operator } from \"opendal\";\n\nconst operator =
new Operator(\"s3\", {\n // --- General ---\n // root of this backend.\n
//\n // All operations will happen under this root.\n //\n // default to `/`
if not set.\n // root: \"/\",\n\n // bucket name of this backend.\n //\n //
required.\n bucket: \"my-bucket\",\n\n // endpoint of this backend.\n //\n
// Endpoint must be full uri, e.g.\n //\n // - AWS S3:
`https://s3.amazonaws.com` or `https://s3.{ [...]
+ "full": "import { Operator } from \"opendal\";\n\nconst operator =
new Operator(\"s3\", {\n // --- General ---\n // root of this backend.\n
//\n // All operations will happen under this root.\n //\n // default to `/`
if not set.\n // root: \"/\",\n\n // bucket name of this backend.\n //\n //
required.\n bucket: \"my-bucket\",\n\n // endpoint of this backend.\n //\n
// Endpoint must be full uri, e.g.\n //\n // - AWS S3:
`https://s3.amazonaws.com` or `https://s3.{ [...]
},
{
"binding": "ruby",
"language": "ruby",
"minimal": "require \"opendal\"\n\noperator =
OpenDal::Operator.new(\"s3\", {\n \"bucket\" => \"my-bucket\",\n})",
- "full": "require \"opendal\"\n\noperator =
OpenDal::Operator.new(\"s3\", {\n # --- General ---\n # root of this
backend.\n #\n # All operations will happen under this root.\n #\n #
default to `/` if not set.\n # \"root\" => \"/\",\n\n # bucket name of this
backend.\n #\n # required.\n \"bucket\" => \"my-bucket\",\n\n # endpoint of
this backend.\n #\n # Endpoint must be full uri, e.g.\n #\n # - AWS S3:
`https://s3.amazonaws.com` or `https://s3.{region}.amazonaws. [...]
+ "full": "require \"opendal\"\n\noperator =
OpenDal::Operator.new(\"s3\", {\n # --- General ---\n # root of this
backend.\n #\n # All operations will happen under this root.\n #\n #
default to `/` if not set.\n # \"root\" => \"/\",\n\n # bucket name of this
backend.\n #\n # required.\n \"bucket\" => \"my-bucket\",\n\n # endpoint of
this backend.\n #\n # Endpoint must be full uri, e.g.\n #\n # - AWS S3:
`https://s3.amazonaws.com` or `https://s3.{region}.amazonaws. [...]
},
{
"binding": "go",
"language": "go",
"minimal": "import
(\n\t\"github.com/apache/opendal-go-services/s3\"\n\topendal
\"github.com/apache/opendal/bindings/go\"\n)\n\noperator, err :=
opendal.NewOperator(s3.Scheme, opendal.OperatorOptions{\n\t\"bucket\":
\"my-bucket\",\n})",
- "full": "import
(\n\t\"github.com/apache/opendal-go-services/s3\"\n\topendal
\"github.com/apache/opendal/bindings/go\"\n)\n\noperator, err :=
opendal.NewOperator(s3.Scheme, opendal.OperatorOptions{\n\t// --- General
---\n\t// root of this backend.\n\t//\n\t// All operations will happen under
this root.\n\t//\n\t// default to `/` if not set.\n\t// \"root\":
\"/\",\n\n\t// bucket name of this backend.\n\t//\n\t//
required.\n\t\"bucket\": \"my-bucket\",\n\n\t// endpoint of this ba [...]
+ "full": "import
(\n\t\"github.com/apache/opendal-go-services/s3\"\n\topendal
\"github.com/apache/opendal/bindings/go\"\n)\n\noperator, err :=
opendal.NewOperator(s3.Scheme, opendal.OperatorOptions{\n\t// --- General
---\n\t// root of this backend.\n\t//\n\t// All operations will happen under
this root.\n\t//\n\t// default to `/` if not set.\n\t// \"root\":
\"/\",\n\n\t// bucket name of this backend.\n\t//\n\t//
required.\n\t\"bucket\": \"my-bucket\",\n\n\t// endpoint of this ba [...]
}
]
},
diff --git a/website/src/components/services/ServicePage.jsx
b/website/src/components/services/ServicePage.jsx
index 65d1afad9a..53072022cc 100644
--- a/website/src/components/services/ServicePage.jsx
+++ b/website/src/components/services/ServicePage.jsx
@@ -356,7 +356,10 @@ export default function ServicePage({ data }) {
<p className={styles.hint}>
Every option is passed as a string key; OpenDAL parses it into
the
- right type. Some services may require building the binding with
the
+ right type. For a <code>map</code> option, pass a string
+ containing a JSON object whose values are strings, such
+ as <code>{'{"key":"value"}'}</code>. Some services may require
+ building the binding with the
matching <code>services-*</code> feature enabled.
</p>
</section>