diff --git a/file.go b/file.go index 53eebc8..8581e2e 100644 --- a/file.go +++ b/file.go @@ -2,8 +2,9 @@ package commons import ( "log" - "os" - "path/filepath" + "os" + "strings" + "path/filepath" ) // FileDeepScan will scan dir recursively and append any files found to files. @@ -22,4 +23,27 @@ func FileDeepScan(dir string, files *[]string) { *files = append(*files, filepath.Join(dir, entry.Name())) } } -} \ No newline at end of file +} + +type WorkspaceInfo struct { + ParentDir string +} + +// FileWorkspaceInfo given a file will return relevant information about +// its location, such as its parent directory. +func FileWorkspaceInfo(file string) *WorkspaceInfo { + // 1. break down file + clean := filepath.Clean(file) + parts := strings.Split(clean, string(filepath.Separator)) + + l := len(parts) + + if l > 3 { + parentDir := parts[l - 2] + return &WorkspaceInfo { + ParentDir: parentDir, + } + } + + return nil +} diff --git a/file_test.go b/file_test.go index a96fc4d..e4c3711 100644 --- a/file_test.go +++ b/file_test.go @@ -3,8 +3,26 @@ package commons import ( "testing" "slices" + + "path/filepath" ) +func TestFileWorkspaceInfo(t *testing.T) { + abs, err := filepath.Abs(".file/dir1/file1.json") + if err != nil { + t.Fatalf("%v", err) + } + + w := FileWorkspaceInfo(abs) + if w == nil { + t.Fatal("should have returned") + } + + if w.ParentDir != "dir1" { + t.Fatalf("invalid parent dir %v", w) + } +} + func TestFileDeepScan(t *testing.T) { files := make([]string, 0) diff --git a/string_test.go b/string_test.go index 0a4ed24..740b7e0 100644 --- a/string_test.go +++ b/string_test.go @@ -4,6 +4,15 @@ import ( "testing" ) +func TestStringTitleCamel(t *testing.T) { + v := "actionObject" + + vv := StringTitle(v) + + if vv != "ActionObject" { + t.Fatalf("Failed to capitalize %s", vv) + } +} func TestStringTitle(t *testing.T) { v := "string to make title" diff --git a/strings.go b/strings.go index adb0813..f7620e4 100644 --- a/strings.go +++ b/strings.go @@ -8,7 +8,7 @@ import ( "golang.org/x/text/language" ) -var caserTitle cases.Caser = cases.Title(language.English) +var caserTitle cases.Caser = cases.Title(language.English, cases.NoLower) var whitespace = regexp.MustCompile(`\s+`) diff --git a/struct.go b/struct.go index 32052d0..3d41206 100644 --- a/struct.go +++ b/struct.go @@ -4,11 +4,33 @@ import ( "log" "reflect" "strings" + "encoding/json" "github.com/go-viper/mapstructure/v2" "go.mongodb.org/mongo-driver/v2/bson" ) +// StructMustTomap given any struct return the equivalent map[string]any or nil. +// Will never throw. +func StructMustToMap(data any) map[string]any { + b, err := json.Marshal(data) + if err != nil { + log.Printf("Failed marshal %v", err) + + return nil + } + + var res map[string]any + err = json.Unmarshal(b, &res) + if err != nil { + log.Printf("Failed to unmarshal %v", err) + + return nil + } + + return res +} + // StructSetValue will update the value of the given field of struct o. func StructSetValue(o any, field string, value any) { ref := reflect.ValueOf(o).Elem() @@ -30,7 +52,6 @@ func StructSetValue(o any, field string, value any) { } } - func StructToStruct(source any, target any) { mapstructure.Decode(source, &target) }