github.com/nevalang/neva@v0.23.1-0.20240507185603-7696a9bb8dda/internal/compiler/utils.go (about) 1 package compiler 2 3 import ( 4 "encoding/json" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "github.com/nevalang/neva/internal/compiler/sourcecode/core" 10 ) 11 12 // Pointer allows to avoid creating of temporary variables just to take pointers. 13 func Pointer[T any](v T) *T { 14 return &v 15 } 16 17 // ParseEntityRef assumes string-ref has form of <pkg_name>.<entity_nameā„ or just <entity_name>. 18 func ParseEntityRef(ref string) core.EntityRef { 19 entityRef := core.EntityRef{ 20 Meta: core.Meta{Text: ref}, 21 } 22 23 parts := strings.Split(ref, ".") 24 if len(parts) == 2 { 25 entityRef.Pkg = parts[0] 26 entityRef.Name = parts[1] 27 } else { 28 entityRef.Name = ref 29 } 30 31 return entityRef 32 } 33 34 // JSONDump is for debugging purposes only! 35 func JSONDump(v any) string { 36 bb, err := json.Marshal(v) 37 if err != nil { 38 return err.Error() 39 } 40 return string(bb) 41 } 42 43 func SaveFilesToDir(dst string, files map[string][]byte) error { 44 for path, content := range files { 45 filePath := filepath.Join(dst, path) 46 dirPath := filepath.Dir(filePath) 47 48 if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { 49 return err 50 } 51 52 if err := os.WriteFile(filePath, content, os.ModePerm); err != nil { 53 return err 54 } 55 } 56 57 return nil 58 }