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 1 commit
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/xdr2json"
"lib/xdr2json",
"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 @@ -23,5 +23,8 @@ build-libs: Cargo.lock
cp $(BUILD_DIR)/$$target/$(PROFILE)/*.a $(LIBS_DIR)/$$target/; \
done

generate-types:
cargo run --bin generate-types | gofmt > xdr2json/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 xdr2json");
println!();
println!("type XdrType string");
println!();
println!("const (");
for typ in TypeVariant::variants() {
let name = typ.name();
println!(" {name} XdrType = \"{name}\"");
}
println!(")");
}
44 changes: 4 additions & 40 deletions xdr2json/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,18 @@ package xdr2json
import "C"

import (
"encoding"
"encoding/json"
"github.com/pkg/errors"
"reflect"
"unsafe"
)

// 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)
}
"github.com/pkg/errors"
)

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
10 changes: 3 additions & 7 deletions xdr2json/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import (
"github.com/stretchr/testify/require"
)

// We just need similarly named structs as are defined in the
type Asset struct{}
type 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 @@ -25,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 @@ -45,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))
}
467 changes: 467 additions & 0 deletions xdr2json/types.go

Large diffs are not rendered by default.