Skip to content

feat: implement abstract GoStruct base class to reduce generated code verbosity #76

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

Closed
Closed
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
9 changes: 8 additions & 1 deletion compiler/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,18 @@ func shouldApplyClone(pkg *packages.Package, rhs ast.Expr) bool {
return false
}

// Optimization: If it's a composite literal for a struct, no need to clone
// Optimizations: Don't clone for expressions that already produce fresh values

// If it's a composite literal for a struct, no need to clone
// as it's already a fresh value
if _, isCompositeLit := rhs.(*ast.CompositeLit); isCompositeLit {
return false
}

// If it's a function call, no need to clone as function results are already fresh values
if _, isCallExpr := rhs.(*ast.CallExpr); isCallExpr {
return false
}

// Check if it's a struct type (directly, through named type, or underlying)
if named, ok := exprType.(*types.Named); ok {
Expand Down
10 changes: 8 additions & 2 deletions compiler/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *GoToTSCompiler) WriteIndexExpr(exp *ast.IndexExpr) error {
underlyingType := tv.Type.Underlying()
// Check if it's a map type
if mapType, isMap := underlyingType.(*types.Map); isMap {
c.tsw.WriteLiterally("$.mapGet(")
c.tsw.WriteLiterally("($.mapGet(")
if err := c.WriteValueExpr(exp.X); err != nil {
return err
}
Expand All @@ -52,7 +52,13 @@ func (c *GoToTSCompiler) WriteIndexExpr(exp *ast.IndexExpr) error {

// Generate the zero value as the default value for mapGet
c.WriteZeroValueForType(mapType.Elem())
c.tsw.WriteLiterally(")[0]") // Extract the value from the tuple
c.tsw.WriteLiterally(")[0]")

c.tsw.WriteLiterally(" as ")
c.WriteGoType(mapType.Elem(), GoTypeContextGeneral)

c.tsw.WriteLiterally(")")

return nil
}

Expand Down
45 changes: 45 additions & 0 deletions compiler/field-descriptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package compiler

import (
"go/types"
)

func (c *GoToTSCompiler) writeFieldDescriptor(fieldName string, fieldType types.Type, isEmbedded bool) {
c.tsw.WriteLiterallyf("%s: { type: ", fieldName)

switch t := fieldType.Underlying().(type) {
case *types.Basic:
switch t.Kind() {
case types.String:
c.tsw.WriteLiterally("String")
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64,
types.Float32, types.Float64:
c.tsw.WriteLiterally("Number")
case types.Bool:
c.tsw.WriteLiterally("Boolean")
default:
c.tsw.WriteLiterally("Object")
}
default:
c.tsw.WriteLiterally("Object")
}

c.tsw.WriteLiterally(", default: ")

if isEmbedded {
_, isPtr := fieldType.(*types.Pointer)
_, isInterface := fieldType.Underlying().(*types.Interface)
if isPtr || isInterface {
c.tsw.WriteLiterally("null")
} else {
typeForNew := c.getEmbeddedFieldKeyName(fieldType)
c.tsw.WriteLiterallyf("new %s()", typeForNew)
}
c.tsw.WriteLiterally(", isEmbedded: true")
} else {
c.WriteZeroValueForType(fieldType)
}

c.tsw.WriteLiterally(" }")
}
Loading
Loading