Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple from stellar/go monorepo #9

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
resolver = "2"
members = [
"lib/ffi",
"lib/xdrjson"
"lib/xdrjson",
"scripts/generate-types"
]

[workspace.package]
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ build-libs: Cargo.lock
shasum -a 256 $(LIBS_DIR)/**/*.a; \
'

generate-types:
cargo run --bin generate-types | gofmt > xdrjson/types.go

dist-clean:
@rm -rf $(BUILD_DIR) $(LIBS_DIR)
9 changes: 9 additions & 0 deletions scripts/generate-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "generate-types"
version = "0.0.0"
edition = "2021"
rust-version.workspace = true
publish = false

[dependencies]
stellar-xdr = { workspace = true }
18 changes: 18 additions & 0 deletions scripts/generate-types/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use stellar_xdr::curr::TypeVariant;

/// Generates a Go file that contains the types for the XDR-JSON schema as
/// constants.
fn main() {
println!("// Code generated by scripts/generate-types. DO NOT EDIT.");
println!();
println!("package xdrjson");
println!();
println!("type XdrType string");
println!();
println!("const (");
for typ in TypeVariant::variants() {
let name = typ.name();
println!(" {name} XdrType = \"{name}\"");
}
println!(")");
}
41 changes: 2 additions & 39 deletions xdrjson/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,18 @@ package xdrjson
import "C"

import (
"encoding"
"encoding/json"
"reflect"
"unsafe"

"github.com/pkg/errors"
)

// ConvertBytes takes an XDR object (`xdr`) and its serialized bytes (`field`)
// and returns the raw JSON-formatted serialization of that object.
// It can be unmarshalled to a proper JSON structure, but the raw bytes are
// returned to avoid unnecessary round-trips. If there is an
// error, it returns an empty string.
//
// The `xdr` object does not need to actually be initialized/valid:
// we only use it to determine the name of the structure. We could just
// accept a string, but that would make mistakes likelier than passing the
// structure itself (by reference).
func ConvertBytes(xdr interface{}, field []byte) (json.RawMessage, error) {
if len(field) == 0 {
return []byte(""), nil
}

xdrTypeName := reflect.TypeOf(xdr).Name()
return convertAnyBytes(xdrTypeName, field)
}

// ConvertInterface takes a valid XDR object (`xdr`) and returns
// the raw JSON-formatted serialization of that object. If there is an
// error, it returns an empty string.
//
// Unlike `ConvertBytes`, the value here needs to be valid and
// serializable.
func ConvertInterface(xdr encoding.BinaryMarshaler) (json.RawMessage, error) {
xdrTypeName := reflect.TypeOf(xdr).Name()
data, err := xdr.MarshalBinary()
if err != nil {
return []byte(""), errors.Wrapf(err, "failed to serialize XDR type '%s'", xdrTypeName)
}

return convertAnyBytes(xdrTypeName, data)
}
Comment on lines -37 to -60
Copy link
Member Author

@leighmcculloch leighmcculloch Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConvertBytes and ConvertInterface behave differently:

  • ConvertBytes returns an empty string ("") if the []byte is length zero.
  • ConvertInterface returns an error if the []byte is length zero, because the XDR decoder will error in that case for all XDR types.

@stellar/platform-eng Which is the correct behaviour? I think one must be a bug since both provide a different interface into the same capabilities.

There's a test at least that expects ConvertBytes to return an empty string, so seems some thought went into a use case that wanted an empty string.

Imo it'd be better if the function returned an error, because any other XDR decoder would return an error in this case, and to not return an error is silently hiding what is probably a problem. A system using this library can always translate the error into an empty string if it wants to hide errors.

Thoughts?


func convertAnyBytes(xdrTypeName string, field []byte) (json.RawMessage, error) {
func Decode(xdrTypeName XdrType, field []byte) (json.RawMessage, error) {
var jsonStr, errStr string
// scope just added to show matching alloc/frees
{
goRawXdr := CXDR(field)
b := C.CString(xdrTypeName)
b := C.CString(string(xdrTypeName))

result := C.xdr_to_json(b, goRawXdr)
C.free(unsafe.Pointer(b))
Expand Down
12 changes: 3 additions & 9 deletions xdrjson/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import (
"github.com/stretchr/testify/require"
)

// We just need similarly named structs as are defined in the
type (
Asset struct{}
SorobanTransactionData struct{}
)

func TestConversion(t *testing.T) {
func TestDecode(t *testing.T) {
/* The base64-encoded string representing the asset
Created with:
$ stellar xdr encode --type Asset << -
Expand All @@ -27,7 +21,7 @@ func TestConversion(t *testing.T) {
rawBytes, err := base64.StdEncoding.DecodeString(encodedAsset)
require.NoError(t, err)

jsb, err := ConvertBytes(Asset{}, rawBytes)
jsb, err := Decode(Asset, rawBytes)
require.NoError(t, err)

var dest map[string]interface{}
Expand All @@ -47,7 +41,7 @@ func TestConversion(t *testing.T) {
}

func TestEmptyConversion(t *testing.T) {
js, err := ConvertBytes(SorobanTransactionData{}, []byte{})
js, err := Decode(SorobanTransactionData, []byte{})
require.NoError(t, err)
require.Equal(t, "", string(js))
}
2 changes: 1 addition & 1 deletion xdrjson/testcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
os.Exit(1)
}

json, err := xdrjson.ConvertBytes(Asset{}, rawBytes)
json, err := xdrjson.Decode(xdrjson.Asset, rawBytes)
if err != nil {
fmt.Fprintf(os.Stderr, "error: converting binary to json: %v\n", err)
os.Exit(1)
Expand Down
Loading
Loading