feat(x/tx): add an option to encode maps using amino json (#23513)

This commit is contained in:
John Letey 2025-01-27 14:01:23 +00:00 committed by GitHub
parent a60f3b6eab
commit b6d3d72fee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -38,6 +38,8 @@ type EncoderOptions struct {
// It is useful when using the Amino JSON encoder for non Amino purposes,
// such as JSON RPC.
AminoNameAsTypeURL bool
// MarshalMappings when set will use the Amino JSON encoder to marshal maps.
MarshalMappings bool
// TypeResolver is used to resolve protobuf message types by TypeURL when marshaling any packed messages.
TypeResolver signing.TypeResolver
// FileResolver is used to resolve protobuf file descriptors TypeURL when TypeResolver fails.
@ -57,6 +59,7 @@ type Encoder struct {
indent string
enumsAsString bool
aminoNameAsTypeURL bool
marshalMappings bool
}
// NewEncoder returns a new Encoder capable of serializing protobuf messages to JSON using the Amino JSON encoding
@ -93,6 +96,7 @@ func NewEncoder(options EncoderOptions) Encoder {
indent: options.Indent,
enumsAsString: options.EnumAsString,
aminoNameAsTypeURL: options.AminoNameAsTypeURL,
marshalMappings: options.MarshalMappings,
}
return enc
}
@ -237,6 +241,9 @@ func (enc Encoder) marshal(value protoreflect.Value, fd protoreflect.FieldDescri
return err
case protoreflect.Map:
if enc.marshalMappings {
return jsonMarshal(writer, value)
}
return errors.New("maps are not supported")
case protoreflect.List: