Add FileWorkspaceInfo
This commit is contained in:
30
file.go
30
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()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
18
file_test.go
18
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)
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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+`)
|
||||
|
||||
|
||||
23
struct.go
23
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user