Add FileWorkspaceInfo

This commit is contained in:
George Suntres
2026-04-01 20:40:25 -04:00
parent 21fb60b7a9
commit e732870865
5 changed files with 77 additions and 5 deletions

30
file.go
View File

@@ -2,8 +2,9 @@ package commons
import ( import (
"log" "log"
"os" "os"
"path/filepath" "strings"
"path/filepath"
) )
// FileDeepScan will scan dir recursively and append any files found to files. // 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())) *files = append(*files, filepath.Join(dir, entry.Name()))
} }
} }
} }
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
}

View File

@@ -3,8 +3,26 @@ package commons
import ( import (
"testing" "testing"
"slices" "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) { func TestFileDeepScan(t *testing.T) {
files := make([]string, 0) files := make([]string, 0)

View File

@@ -4,6 +4,15 @@ import (
"testing" "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) { func TestStringTitle(t *testing.T) {
v := "string to make title" v := "string to make title"

View File

@@ -8,7 +8,7 @@ import (
"golang.org/x/text/language" "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+`) var whitespace = regexp.MustCompile(`\s+`)

View File

@@ -4,11 +4,33 @@ import (
"log" "log"
"reflect" "reflect"
"strings" "strings"
"encoding/json"
"github.com/go-viper/mapstructure/v2" "github.com/go-viper/mapstructure/v2"
"go.mongodb.org/mongo-driver/v2/bson" "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. // StructSetValue will update the value of the given field of struct o.
func StructSetValue(o any, field string, value any) { func StructSetValue(o any, field string, value any) {
ref := reflect.ValueOf(o).Elem() ref := reflect.ValueOf(o).Elem()
@@ -30,7 +52,6 @@ func StructSetValue(o any, field string, value any) {
} }
} }
func StructToStruct(source any, target any) { func StructToStruct(source any, target any) {
mapstructure.Decode(source, &target) mapstructure.Decode(source, &target)
} }