Skip to content

Commit 6475d31

Browse files
committed
Add possibility to get field directly on struct initialization
1 parent c3ac7ed commit 6475d31

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

parser/parser.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3405,7 +3405,17 @@ func (p *Parser) evaluateStructInitialization(ctx context) (Expression, error) {
34053405
if err != nil {
34063406
return nil, err
34073407
}
3408-
return structInitialization, nil
3408+
var expr Expression = structInitialization
3409+
nextToken = p.peek()
3410+
3411+
if nextToken.Type() == lexer.DOT {
3412+
expr, _, err = p.evaluateStructFieldsFromExpression(expr, nextToken, "", false, ctx) // TODO: Find out if importAlias must be passed correctly.
3413+
3414+
if err != nil {
3415+
return nil, err
3416+
}
3417+
}
3418+
return expr, nil
34093419
}
34103420

34113421
func (p *Parser) evaluateSubscript(ctx context) (Expression, error) {

tests/struct.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ func testDeclareAndDefineStructWithValuesOneLineSuccess(t *testing.T, transpiler
6060
})
6161
}
6262

63+
func testGetFieldDirectlyFromStructInitializationSuccess(t *testing.T, transpilerFunc transpilerFunc) {
64+
transpilerFunc(t, `
65+
type myStruct struct {
66+
a, b string
67+
c bool
68+
d int
69+
}
70+
print(myStruct{a: "Hello", b: "World"}.b)
71+
`, func(output string, err error) {
72+
require.Nil(t, err)
73+
require.Equal(t, "World", output)
74+
})
75+
}
76+
6377
func testDeclareAndDefineStructSliceSuccess(t *testing.T, transpilerFunc transpilerFunc) {
6478
transpilerFunc(t, `
6579
type myStruct struct {

tests/struct_linux_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ func TestDeclareAndDefineStructWithValuesOneLineSuccess(t *testing.T) {
1616
testDeclareAndDefineStructWithValuesOneLineSuccess(t, transpileBash)
1717
}
1818

19+
func TestGetFieldDirectlyFromStructInitializationSuccess(t *testing.T) {
20+
testGetFieldDirectlyFromStructInitializationSuccess(t, transpileBash)
21+
}
22+
1923
func TestDeclareAndDefineStructSliceSuccess(t *testing.T) {
2024
testDeclareAndDefineStructSliceSuccess(t, transpileBash)
2125
}

tests/struct_windows_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ func TestDeclareAndDefineStructWithValuesOneLineSuccess(t *testing.T) {
1616
testDeclareAndDefineStructWithValuesOneLineSuccess(t, transpileBatch)
1717
}
1818

19+
func TestGetFieldDirectlyFromStructInitializationSuccess(t *testing.T) {
20+
testGetFieldDirectlyFromStructInitializationSuccess(t, transpileBatch)
21+
}
22+
1923
func TestDeclareAndDefineStructSliceSuccess(t *testing.T) {
2024
testDeclareAndDefineStructSliceSuccess(t, transpileBatch)
2125
}

0 commit comments

Comments
 (0)