-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from projectdiscovery/feat-memoizer
AST + runtime memoizer
- Loading branch information
Showing
10 changed files
with
750 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/projectdiscovery/utils/memoize" | ||
) | ||
|
||
func main() { | ||
out, err := memoize.File(memoize.PackageTemplate, "../tests/test.go", "test") | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Println(string(out)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// this small cli tool is specific for those functions with arbitrary parameters and with result-error tuple as return values | ||
// func(x,y) => result, error | ||
// it works by creating a new memoized version of the functions in the same path as memo.original.file.go | ||
// some parts are specific for nuclei and hardcoded within the template | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"io/fs" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/projectdiscovery/utils/memoize" | ||
stringsutil "github.com/projectdiscovery/utils/strings" | ||
) | ||
|
||
var ( | ||
src = flag.String("src", "", "go sources") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
err := filepath.Walk(*src, walk) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func walk(path string, info fs.FileInfo, err error) error { | ||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
ext := filepath.Ext(path) | ||
base := filepath.Base(path) | ||
|
||
if !stringsutil.EqualFoldAny(ext, ".go") { | ||
return nil | ||
} | ||
|
||
basePath := filepath.Dir(path) | ||
outPath := filepath.Join(basePath, "memo."+base) | ||
|
||
// filename := filepath.Base(path) | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
if !stringsutil.ContainsAnyI(string(data), "@memo") { | ||
return nil | ||
} | ||
out, err := memoize.Src(memoize.PackageTemplate, path, data, "test") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := os.WriteFile(outPath, out, os.ModePerm); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.