Skip to content

Commit

Permalink
setup go transform
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Nov 1, 2024
1 parent cf452e4 commit dedaa19
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
6 changes: 3 additions & 3 deletions sdk/go/tools/modus-go-build/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ func CollectProgramInfo(config *config.Config, meta *metadata.Metadata, wasmFunc

for name, f := range getExportedFunctions(pkgs) {
if _, ok := wasmFunctions.Exports[name]; ok {
meta.FnExports[name] = transformFunc(name, f)
meta.FnExports[name] = transformFunc(name, f, pkgs)
findRequiredTypes(f, requiredTypes)
}
}

for name, f := range getImportedFunctions(pkgs) {
if _, ok := wasmFunctions.Imports[name]; ok {
meta.FnImports[name] = transformFunc(name, f)
meta.FnImports[name] = transformFunc(name, f, pkgs)
findRequiredTypes(f, requiredTypes)
}
}

// proxy imports overwrite regular imports
for name, f := range getProxyImportFunctions(pkgs) {
if _, ok := meta.FnImports[name]; ok {
meta.FnImports[name] = transformFunc(name, f)
meta.FnImports[name] = transformFunc(name, f, pkgs)
findRequiredTypes(f, requiredTypes)
}
}
Expand Down
26 changes: 26 additions & 0 deletions sdk/go/tools/modus-go-build/extractor/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"strings"

"github.com/hypermodeinc/modus/sdk/go/tools/modus-go-build/metadata"
"golang.org/x/tools/go/packages"
)

Expand All @@ -26,6 +27,31 @@ var wellKnownTypes = map[string]bool{
"time.Duration": true,
}

func getFuncDocumentation(pkgs map[string]*packages.Package, fn *types.Func) *metadata.Docs {
var comments []string

for _, pkg := range pkgs {
for _, file := range pkg.Syntax {
for _, decl := range file.Decls {
if fd, ok := decl.(*ast.FuncDecl); ok {
if fd.Name.Name == fn.Name() {
if fd.Doc != nil {
for _, comment := range fd.Doc.List {
comments = append(comments, comment.Text)
}
}
return &metadata.Docs{
Description: strings.Join(comments, "\n"),
}
}
}
}
}
}
return nil
}


func getExportedFunctions(pkgs map[string]*packages.Package) map[string]*types.Func {
results := make(map[string]*types.Func)
for _, pkg := range pkgs {
Expand Down
4 changes: 3 additions & 1 deletion sdk/go/tools/modus-go-build/extractor/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/hypermodeinc/modus/sdk/go/tools/modus-go-build/metadata"
"github.com/hypermodeinc/modus/sdk/go/tools/modus-go-build/utils"
"golang.org/x/tools/go/packages"
)

func transformStruct(name string, s *types.Struct) *metadata.TypeDefinition {
Expand All @@ -37,7 +38,7 @@ func transformStruct(name string, s *types.Struct) *metadata.TypeDefinition {
}
}

func transformFunc(name string, f *types.Func) *metadata.Function {
func transformFunc(name string, f *types.Func, pkgs map[string]*packages.Package) *metadata.Function {
if f == nil {
return nil
}
Expand All @@ -48,6 +49,7 @@ func transformFunc(name string, f *types.Func) *metadata.Function {

ret := metadata.Function{
Name: name,
Docs: getFuncDocumentation(pkgs, f),
}

if params != nil {
Expand Down
6 changes: 6 additions & 0 deletions sdk/go/tools/modus-go-build/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ type Metadata struct {
Types TypeMap `json:"types,omitempty"`
}

type Docs struct {
Description string `json:"description"`
}

type Function struct {
Name string `json:"-"`
Parameters []*Parameter `json:"parameters,omitempty"`
Results []*Result `json:"results,omitempty"`
Docs *Docs `json:"docs,omitempty"`
}

type TypeDefinition struct {
Id uint32 `json:"id"`
Name string `json:"-"`
Fields []*Field `json:"fields,omitempty"`
Docs *Docs `json:"docs,omitempty"`
}

type Parameter struct {
Expand Down

0 comments on commit dedaa19

Please sign in to comment.