msggen: add string_map type

This commit is contained in:
daywalker90
2025-12-22 13:30:18 +01:00
committed by Rusty Russell
parent ec7d247f38
commit 8bc2e76f44
7 changed files with 22 additions and 1 deletions

View File

@@ -130,6 +130,8 @@ class GrpcConverterGenerator(IGenerator):
"TlvStream?": f"c.{name}.map(|s| s.into())", "TlvStream?": f"c.{name}.map(|s| s.into())",
"RoutehintList?": f"c.{name}.map(|rl| rl.into())", "RoutehintList?": f"c.{name}.map(|rl| rl.into())",
"DecodeRoutehintList?": f"c.{name}.map(|drl| drl.into())", "DecodeRoutehintList?": f"c.{name}.map(|drl| drl.into())",
"string_map": f"Some(c.{name})",
"string_map?": f"c.{name}.unwrap_or(HashMap::new())",
}.get( }.get(
typ, f"c.{name}" # default to just assignment typ, f"c.{name}" # default to just assignment
) )
@@ -202,6 +204,7 @@ class GrpcConverterGenerator(IGenerator):
use cln_rpc::notifications; use cln_rpc::notifications;
use crate::pb; use crate::pb;
use std::str::FromStr; use std::str::FromStr;
use std::collections::HashMap;
use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash; use bitcoin::hashes::Hash;
use cln_rpc::primitives::PublicKey; use cln_rpc::primitives::PublicKey;

View File

@@ -201,7 +201,7 @@ class GrpcGenerator(IGenerator):
if f.omit(): if f.omit():
continue continue
opt = "optional " if f.optional else "" opt = "optional " if f.optional and not (isinstance(f, PrimitiveField) and f.typename == "string_map") else ""
if isinstance(f, ArrayField): if isinstance(f, ArrayField):
typename = f.override( typename = f.override(

View File

@@ -131,6 +131,8 @@ class GrpcUnconverterGenerator(GrpcConverterGenerator):
"hash?": f"c.{name}.map(|v| Sha256::from_slice(&v).unwrap())", "hash?": f"c.{name}.map(|v| Sha256::from_slice(&v).unwrap())",
"txid": f"hex::encode(&c.{name})", "txid": f"hex::encode(&c.{name})",
"TlvStream?": f"c.{name}.map(|s| s.into())", "TlvStream?": f"c.{name}.map(|s| s.into())",
"string_map": f"c.{name}.unwrap()",
"string_map?": f"Some(c.{name})",
}.get( }.get(
typ, f"c.{name}" # default to just assignment typ, f"c.{name}" # default to just assignment
) )

View File

@@ -35,6 +35,7 @@ typemap = {
"secret": "bytes", "secret": "bytes",
"bip340sig": "string", "bip340sig": "string",
"hash": "bytes", "hash": "bytes",
"string_map": "map<string, string>",
} }

View File

@@ -46,6 +46,7 @@ typemap = {
"secret": "Secret", "secret": "Secret",
"bip340sig": "String", "bip340sig": "String",
"integer": "i64", "integer": "i64",
"string_map": "HashMap<String, String>",
} }
header = f""" header = f"""
@@ -306,6 +307,7 @@ class RustGenerator(IGenerator):
#[allow(unused_imports)] #[allow(unused_imports)]
use serde::{{Deserialize, Serialize}}; use serde::{{Deserialize, Serialize}};
use core::fmt::Debug; use core::fmt::Debug;
use std::collections::HashMap;
use super::{IntoRequest, Request, TypedRequest}; use super::{IntoRequest, Request, TypedRequest};
""" """
) )

View File

@@ -432,6 +432,7 @@ class PrimitiveField(Field):
"secret", "secret",
"bip340sig", "bip340sig",
"hash", "hash",
"string_map",
] ]
def __init__(self, typename, path, description, added, deprecated): def __init__(self, typename, path, description, added, deprecated):

View File

@@ -411,6 +411,17 @@ def _extra_validator(is_request: bool):
return True return True
return False return False
def is_string_map(checker, instance):
"""key, value map with strings"""
if not checker.is_type(instance, "object"):
return False
for k, v in instance.items():
if not checker.is_type(k, "string"):
return False
if not checker.is_type(v, "string"):
return False
return True
# "msat" for request can be many forms # "msat" for request can be many forms
if is_request: if is_request:
is_msat = is_msat_request is_msat = is_msat_request
@@ -439,6 +450,7 @@ def _extra_validator(is_request: bool):
"outpoint": is_outpoint, "outpoint": is_outpoint,
"feerate": is_feerate, "feerate": is_feerate,
"outputdesc": is_outputdesc, "outputdesc": is_outputdesc,
"string_map": is_string_map,
}) })
return jsonschema.validators.extend(jsonschema.Draft7Validator, return jsonschema.validators.extend(jsonschema.Draft7Validator,