Skip to content

Commit

Permalink
Updated golang ext generator test to prevent regression
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyaxod committed Sep 19, 2023
1 parent b70efb4 commit c73106a
Show file tree
Hide file tree
Showing 6 changed files with 561 additions and 17 deletions.
270 changes: 270 additions & 0 deletions extension/generator/golang/generated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
// Code generated by scale-signature v0.3.20, DO NOT EDIT.
// output: types

package types

import (
"errors"
"github.com/loopholelabs/polyglot"
)

var (
NilDecode = errors.New("cannot decode into a nil root struct")
InvalidEnum = errors.New("invalid enum value")
)

type HttpConfig struct {
Timeout int32
}

func NewHttpConfig() *HttpConfig {
return &HttpConfig{

Timeout: 60,
}
}

func (x *HttpConfig) Encode(b *polyglot.Buffer) {
e := polyglot.Encoder(b)
if x == nil {
e.Nil()
} else {

e.Int32(x.Timeout)

}
}

func DecodeHttpConfig(x *HttpConfig, b []byte) (*HttpConfig, error) {
d := polyglot.GetDecoder(b)
defer d.Return()
return _decodeHttpConfig(x, d)
}

func _decodeHttpConfig(x *HttpConfig, d *polyglot.Decoder) (*HttpConfig, error) {
if d.Nil() {
return nil, nil
}

err, _ := d.Error()
if err != nil {
return nil, err
}

if x == nil {
x = NewHttpConfig()
}

x.Timeout, err = d.Int32()
if err != nil {
return nil, err
}

return x, nil
}

type HttpResponse struct {
Headers map[string]StringList

StatusCode int32

Body []byte
}

func NewHttpResponse() *HttpResponse {
return &HttpResponse{

Headers: make(map[string]StringList),

StatusCode: 0,

Body: make([]byte, 0, 0),
}
}

func (x *HttpResponse) Encode(b *polyglot.Buffer) {
e := polyglot.Encoder(b)
if x == nil {
e.Nil()
} else {

e.Map(uint32(len(x.Headers)), polyglot.StringKind, polyglot.AnyKind)
for k, v := range x.Headers {
e.String(k)
v.Encode(b)
}

e.Int32(x.StatusCode)

e.Bytes(x.Body)

}
}

func DecodeHttpResponse(x *HttpResponse, b []byte) (*HttpResponse, error) {
d := polyglot.GetDecoder(b)
defer d.Return()
return _decodeHttpResponse(x, d)
}

func _decodeHttpResponse(x *HttpResponse, d *polyglot.Decoder) (*HttpResponse, error) {
if d.Nil() {
return nil, nil
}

err, _ := d.Error()
if err != nil {
return nil, err
}

if x == nil {
x = NewHttpResponse()
}

mapSizeHeaders, err := d.Map(polyglot.StringKind, polyglot.AnyKind)
if err != nil {
return nil, err
}

if uint32(len(x.Headers)) != mapSizeHeaders {
x.Headers = make(map[string]StringList, mapSizeHeaders)
}

for i := uint32(0); i < mapSizeHeaders; i++ {
k, err := d.String()
if err != nil {
return nil, err
}
v, err := _decodeStringList(nil, d)
if err != nil {
return nil, err
}
x.Headers[k] = *v
}

x.StatusCode, err = d.Int32()
if err != nil {
return nil, err
}

x.Body, err = d.Bytes(nil)
if err != nil {
return nil, err
}

return x, nil
}

type StringList struct {
Values []string
}

func NewStringList() *StringList {
return &StringList{

Values: make([]string, 0, 0),
}
}

func (x *StringList) Encode(b *polyglot.Buffer) {
e := polyglot.Encoder(b)
if x == nil {
e.Nil()
} else {

e.Slice(uint32(len(x.Values)), polyglot.StringKind)
for _, a := range x.Values {
e.String(a)
}

}
}

func DecodeStringList(x *StringList, b []byte) (*StringList, error) {
d := polyglot.GetDecoder(b)
defer d.Return()
return _decodeStringList(x, d)
}

func _decodeStringList(x *StringList, d *polyglot.Decoder) (*StringList, error) {
if d.Nil() {
return nil, nil
}

err, _ := d.Error()
if err != nil {
return nil, err
}

if x == nil {
x = NewStringList()
}

sliceSizeValues, err := d.Slice(polyglot.StringKind)
if err != nil {
return nil, err
}

if uint32(len(x.Values)) != sliceSizeValues {
x.Values = make([]string, sliceSizeValues)
}

for i := uint32(0); i < sliceSizeValues; i++ {
x.Values[i], err = d.String()
if err != nil {
return nil, err
}
}

return x, nil
}

type ConnectionDetails struct {
Url string
}

func NewConnectionDetails() *ConnectionDetails {
return &ConnectionDetails{

Url: "https://google.com",
}
}

func (x *ConnectionDetails) Encode(b *polyglot.Buffer) {
e := polyglot.Encoder(b)
if x == nil {
e.Nil()
} else {

e.String(x.Url)

}
}

func DecodeConnectionDetails(x *ConnectionDetails, b []byte) (*ConnectionDetails, error) {
d := polyglot.GetDecoder(b)
defer d.Return()
return _decodeConnectionDetails(x, d)
}

func _decodeConnectionDetails(x *ConnectionDetails, d *polyglot.Decoder) (*ConnectionDetails, error) {
if d.Nil() {
return nil, nil
}

err, _ := d.Error()
if err != nil {
return nil, err
}

if x == nil {
x = NewConnectionDetails()
}

x.Url, err = d.String()
if err != nil {
return nil, err
}

return x, nil
}
41 changes: 24 additions & 17 deletions extension/generator/golang/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,41 @@ func TestGenerator(t *testing.T) {

packageName := "extfetch"

f_interfaces, err := GenerateInterfaces(s, packageName, "v0.1.0")
interfaces, err := GenerateInterfaces(s, packageName, "v0.1.0")
require.NoError(t, err)
os.WriteFile("./interfaces.txt", f_interfaces, 0644)
// os.WriteFile("./interfaces.txt", interfaces, 0644)

formatted, err := GenerateTypes(s, "types")
expInterfaces, err := os.ReadFile("./interfaces.txt")
require.NoError(t, err)
require.Equal(t, string(expInterfaces), string(interfaces))

// Check things...

os.WriteFile("./generated.txt", formatted, 0644)

master, err := os.ReadFile("./generated.txt")
formatted, err := GenerateTypes(s, "types")
require.NoError(t, err)
require.Equal(t, string(master), string(formatted))
// os.WriteFile("./generated.txt", formatted, 0644)

f_host, err := GenerateHost(s, packageName, "v0.1.0")
expTypes, err := os.ReadFile("./generated.txt")
require.NoError(t, err)
require.Equal(t, string(expTypes), string(formatted))

os.WriteFile("./host.txt", f_host, 0644)

f_guest, err := GenerateGuest(s, packageName, "v0.1.0")
host, err := GenerateHost(s, packageName, "v0.1.0")
require.NoError(t, err)
// os.WriteFile("./host.txt", host, 0644)
expHost, err := os.ReadFile("./host.txt")
require.NoError(t, err)
require.Equal(t, string(expHost), string(host))

os.WriteFile("./guest.txt", f_guest, 0644)

modf, err := GenerateModfile(packageName)
guest, err := GenerateGuest(s, packageName, "v0.1.0")
require.NoError(t, err)
// os.WriteFile("./guest.txt", guest, 0644)
expGuest, err := os.ReadFile("./guest.txt")
require.NoError(t, err)
require.Equal(t, string(expGuest), string(guest))

os.WriteFile("./modfile.txt", modf, 0644)
mod, err := GenerateModfile(packageName)
require.NoError(t, err)
// os.WriteFile("./modfile.txt", mod, 0644)
expMod, err := os.ReadFile("./modfile.txt")
require.NoError(t, err)
require.Equal(t, string(expMod), string(mod))

}
Loading

0 comments on commit c73106a

Please sign in to comment.