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

add ptr data to struct #26

Merged
merged 8 commits into from
Dec 20, 2021
Merged
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
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ gopium walker package strategy_1 strategy_2 strategy_3 ...

## Requirements Installation and Usage

Gopium uses go1.16, but it's most likely gonna work with other versions too.
Gopium uses go1.17, but it's most likely gonna work with other versions too.
Note that Gopium is heavily relying on [types](https://golang.org/pkg/go/types/) and [ast](https://golang.org/pkg/go/ast/) packages, and these packages might be slightly different among major go releases.

To install Gopium VSCode Extension use [vscode marketplace](https://marketplace.visualstudio.com/items?itemName=1pkg.gopium).
Expand Down
14 changes: 10 additions & 4 deletions collections/size_align_pad.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,22 @@ func WalkStruct(st gopium.Struct, sysalign int64, onpad OnPadFields) {
}
}

// SizeAlign calculates sturct aligned size and size
// SizeAlignPtr calculates sturct aligned size, size and ptr size
// by using walk struct helper
func SizeAlign(st gopium.Struct) (int64, int64) {
func SizeAlignPtr(st gopium.Struct) (int64, int64, int64) {
// preset defaults
var alsize, align int64 = 0, 1
var alsize, align, ptrsize int64 = 0, 1, 0
WalkStruct(st, 0, func(pad int64, fields ...gopium.Field) {
// add pad to aligned size
alsize += pad
// go through fields
for _, f := range fields {
// in case filed has pointer data
// move pts size to last aligned size
// + this field ptr data size
if f.Ptr > 0 {
ptrsize = alsize + f.Ptr
}
// add field size aligned sizes
alsize += f.Size
// update struct align size
Expand All @@ -73,7 +79,7 @@ func SizeAlign(st gopium.Struct) (int64, int64) {
}
}
})
return alsize, align
return alsize, align, ptrsize
}

// PadField defines helper that
Expand Down
21 changes: 16 additions & 5 deletions collections/size_align_pad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"github.com/1pkg/gopium/gopium"
)

func TestSizeAlign(t *testing.T) {
func TestSizeAlignPtr(t *testing.T) {
// prepare
table := map[string]struct {
st gopium.Struct
size int64
align int64
ptr int64
}{
"empty struct should return expected size and align": {
"empty struct should return expected size, align and ptr": {
size: 0,
align: 1,
ptr: 0,
},
"non empty struct should return expected size and align": {
"non empty struct should return expected size, align and ptr": {
st: gopium.Struct{
Name: "test",
Comment: []string{"test"},
Expand All @@ -28,6 +30,7 @@ func TestSizeAlign(t *testing.T) {
Type: "int",
Size: 8,
Align: 4,
Ptr: 8,
},
{
Name: "test2",
Expand All @@ -44,8 +47,9 @@ func TestSizeAlign(t *testing.T) {
},
size: 16,
align: 8,
ptr: 8,
},
"struct with pads should return expected size and align": {
"struct with pads should return expected size, align and ptr": {
st: gopium.Struct{
Name: "test",
Comment: []string{"test"},
Expand All @@ -54,35 +58,42 @@ func TestSizeAlign(t *testing.T) {
Name: "test1",
Size: 3,
Align: 1,
Ptr: 3,
},
{
Name: "test2",
Type: "float64",
Size: 8,
Align: 6,
Ptr: 8,
},
{
Name: "test3",
Size: 3,
Align: 1,
Ptr: 3,
},
},
},
size: 18,
align: 6,
ptr: 17,
},
}
for name, tcase := range table {
t.Run(name, func(t *testing.T) {
// exec
size, align := SizeAlign(tcase.st)
size, align, ptr := SizeAlignPtr(tcase.st)
// check
if !reflect.DeepEqual(size, tcase.size) {
t.Errorf("actual %v doesn't equal to %v", size, tcase.size)
}
if !reflect.DeepEqual(align, tcase.align) {
t.Errorf("actual %v doesn't equal to %v", size, tcase.size)
}
if !reflect.DeepEqual(ptr, tcase.ptr) {
t.Errorf("actual %v doesn't equal to %v", ptr, tcase.ptr)
}
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "Kostiantyn Masliuk - github.com/1pkg"
},
"license": "MIT",
"version": "1.5.0",
"version": "1.6.0",
"categories": [
"Programming Languages",
"Formatters",
Expand Down
3 changes: 2 additions & 1 deletion fmtio/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"go/ast"
"go/token"
"reflect"
Expand Down Expand Up @@ -365,7 +366,7 @@ test struct {// random
if !reflect.DeepEqual(perr, nil) {
t.Errorf("actual %v doesn't equal to expected %v", perr, nil)
}
if !reflect.DeepEqual(err, tcase.err) {
if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tcase.err) {
t.Errorf("actual %v doesn't equal to expected %v", err, tcase.err)
}
// format actual and expected identically
Expand Down
3 changes: 2 additions & 1 deletion fmtio/astutil/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"go/parser"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -301,7 +302,7 @@ type tt gopium.Struct
}
// exec
pkg, err = tcase.a(tcase.ctx, pkg, loc, tcase.h)
if !reflect.DeepEqual(err, tcase.err) {
if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tcase.err) {
t.Errorf("actual %v doesn't equal to expected %v", err, tcase.err)
}
// prepare
Expand Down
3 changes: 2 additions & 1 deletion fmtio/astutil/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -209,7 +210,7 @@ type (
// exec
err = Package{}.Persist(tcase.ctx, tcase.p, tcase.w, loc, pkg)
// check
if !reflect.DeepEqual(err, tcase.err) {
if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tcase.err) {
t.Errorf("actual %v doesn't equal to expected %v", err, tcase.err)
}
// process checks only on success
Expand Down
9 changes: 6 additions & 3 deletions fmtio/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func Csvb(rw io.ReadWriter) gopium.Bytes {
"Field Type",
"Field Size",
"Field Align",
"Field Ptr",
"Field Tag",
"Field Exported",
"Field Embedded",
Expand All @@ -70,6 +71,7 @@ func Csvb(rw io.ReadWriter) gopium.Bytes {
f.Type,
strconv.Itoa(int(f.Size)),
strconv.Itoa(int(f.Align)),
strconv.Itoa(int(f.Ptr)),
f.Tag,
strconv.FormatBool(f.Exported),
strconv.FormatBool(f.Embedded),
Expand Down Expand Up @@ -100,8 +102,8 @@ func Mdtb(sts []gopium.Struct) ([]byte, error) {
// no error should be
// checked as it uses
// buffered writer
_, _ = buf.WriteString("| Struct Name | Struct Doc | Struct Comment | Field Name | Field Type | Field Size | Field Align | Field Tag | Field Exported | Field Embedded | Field Doc | Field Comment |\n")
_, _ = buf.WriteString("| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n")
_, _ = buf.WriteString("| Struct Name | Struct Doc | Struct Comment | Field Name | Field Type | Field Size | Field Align | Field Ptr | Field Tag | Field Exported | Field Embedded | Field Doc | Field Comment |\n")
_, _ = buf.WriteString("| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n")
for _, st := range sts {
// go through all fields
// and write then one by one
Expand All @@ -110,14 +112,15 @@ func Mdtb(sts []gopium.Struct) ([]byte, error) {
// checked as it uses
// buffered writer
_, _ = buf.WriteString(
fmt.Sprintf("| %s | %s | %s | %s | %s | %d | %d | %s | %s | %s | %s | %s |\n",
fmt.Sprintf("| %s | %s | %s | %s | %s | %d | %d | %d | %s | %s | %s | %s | %s |\n",
st.Name,
strings.Join(st.Doc, " "),
strings.Join(st.Comment, " "),
f.Name,
f.Type,
f.Size,
f.Align,
f.Ptr,
f.Tag,
strconv.FormatBool(f.Exported),
strconv.FormatBool(f.Embedded),
Expand Down
Loading