From c0d7adb1a1b28b6b1de9ccd95d4963a8342ccdbc Mon Sep 17 00:00:00 2001 From: ayyansea Date: Fri, 15 Nov 2024 23:15:00 +0300 Subject: feat: add Filters --- .gitignore | 2 +- Makefile | 4 ++-- cmd/filter.go | 49 +++++++++++++++++++++++++++++++++++++++ cmd/filters.go | 46 ++++++++++++++++++++++++++++++++++++ cmd/uptfs.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/uptfs/main.go | 44 ----------------------------------- internal/config/config.go | 56 -------------------------------------------- internal/split/split.go | 44 ----------------------------------- main.go | 7 ++++++ util/config/config.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ util/split/split.go | 45 ++++++++++++++++++++++++++++++++++++ 11 files changed, 265 insertions(+), 147 deletions(-) create mode 100644 cmd/filter.go create mode 100644 cmd/filters.go create mode 100644 cmd/uptfs.go delete mode 100644 cmd/uptfs/main.go delete mode 100644 internal/config/config.go delete mode 100644 internal/split/split.go create mode 100644 main.go create mode 100644 util/config/config.go create mode 100644 util/split/split.go diff --git a/.gitignore b/.gitignore index 15a7c3d..50ea6e5 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,7 @@ go.work.sum .env # binary -./uptfs +uptfs # config config.yaml diff --git a/Makefile b/Makefile index 6451759..0f55e78 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -BINARY_DIR=./cmd/uptfs +BINARY_DIR=./cmd .PHONY: all all: build @@ -6,7 +6,7 @@ all: build .PHONY: build build: @echo "Building Go uptfs..." - go build $(BINARY_DIR) + go build .PHONY: clean clean: diff --git a/cmd/filter.go b/cmd/filter.go new file mode 100644 index 0000000..9d8ccac --- /dev/null +++ b/cmd/filter.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "fmt" +) + +type Filter struct { + name string + action func(string) string + subfilters []Filter +} + +type Greeter interface { + Greet() +} + +type Filterer interface { + Greeter + Filter(string) string +} + +func (f Filter) Greet() { + subfilterCount := len(f.subfilters) + + fmt.Printf("I am a filter and my name is %v\n", f.name) + if subfilterCount > 0 { + fmt.Println("My subfilters are:") + + for _, subfilter := range f.subfilters { + fmt.Printf("- %v\n", subfilter.name) + } + } +} + +func (f Filter) Filter(token string) (modifiedToken string) { + subfilterCount := len(f.subfilters) + modifiedToken = token + + if subfilterCount > 0 { + for _, subfilter := range f.subfilters { + modifiedToken = subfilter.action(modifiedToken) + } + + return modifiedToken + } + + modifiedToken = f.action(token) + return modifiedToken +} diff --git a/cmd/filters.go b/cmd/filters.go new file mode 100644 index 0000000..28592ba --- /dev/null +++ b/cmd/filters.go @@ -0,0 +1,46 @@ +package cmd + +import ( + "strings" +) + +var filterList = map[string]interface{}{ + "uppercase": NewUppercaseFilter, + "lowercase": NewLowercaseFilter, + "lowercase_extra": NewLowercaseFilterWithExtraSteps, +} + +func NewUppercaseFilter() Filter { + uppercaseFilter := Filter{ + name: "Uppercase", + action: strings.ToUpper, + subfilters: []Filter{}, + } + + return uppercaseFilter +} + +func NewLowercaseFilter() Filter { + lowercaseFilter := Filter{ + name: "Lowercase", + action: strings.ToLower, + subfilters: []Filter{}, + } + + return lowercaseFilter +} + +func NewLowercaseFilterWithExtraSteps() Filter { + var subfilters []Filter + + subfilters = append(subfilters, NewUppercaseFilter()) + subfilters = append(subfilters, NewLowercaseFilter()) + + filter := Filter{ + name: "Lowercase (extra dumb)", + action: func(string) string { return "" }, + subfilters: subfilters, + } + + return filter +} diff --git a/cmd/uptfs.go b/cmd/uptfs.go new file mode 100644 index 0000000..1bba4c4 --- /dev/null +++ b/cmd/uptfs.go @@ -0,0 +1,59 @@ +package cmd + +import ( + "bufio" + "errors" + "fmt" + "os" + "strings" + "uptfs/util/split" +) + +func Run() { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + inputString := scanner.Text() + + if inputString == "" { + err := errors.New("the input string is empty") + fmt.Printf("%v\n", err) + os.Exit(1) + } + + additionalDelimeters := []string{",", "."} + tokens := strings.Split(inputString, " ") + tokens = formatInput(tokens, additionalDelimeters) + + lowercaseFilter := NewLowercaseFilterWithExtraSteps() + lowercaseFilter.Greet() + + var newTokens []string + for _, token := range tokens { + newTokens = append(newTokens, lowercaseFilter.Filter(token)) + } + + tokens = newTokens + + uppercaseFilter := NewUppercaseFilter() + uppercaseFilter.Greet() + + newTokens = nil + for _, token := range tokens { + newTokens = append(newTokens, uppercaseFilter.Filter(token)) + } + + result := strings.Join(newTokens, " ") + + fmt.Println("") + fmt.Println(result) +} + +func formatInput(tokenArray []string, delimeterArray []string) []string { + for _, delimeter := range delimeterArray { + for index, element := range tokenArray { + tokenArray = split.NewArrayWithSplit(tokenArray, index, element, delimeter) + } + } + + return tokenArray +} diff --git a/cmd/uptfs/main.go b/cmd/uptfs/main.go deleted file mode 100644 index c8fbf89..0000000 --- a/cmd/uptfs/main.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "bufio" - "errors" - "fmt" - "os" - "strings" - "uptfs/internal/config" - "uptfs/internal/split" -) - -const filepath = "" - -func main() { - var config config.Config - config.LoadConfig(filepath) - - scanner := bufio.NewScanner(os.Stdin) - scanner.Scan() - inputString := scanner.Text() - - if inputString == "" { - err := errors.New("the input string is empty") - fmt.Printf("%v\n", err) - os.Exit(1) - } - - additionalDelimeters := []string{",", "."} - tokens := strings.Split(inputString, " ") - tokens = formatInput(tokens, additionalDelimeters) - - fmt.Println(tokens) -} - -func formatInput(tokenArray []string, delimeterArray []string) []string { - for _, delimeter := range delimeterArray { - for index, element := range tokenArray { - tokenArray = split.NewArrayWithSplit(tokenArray, index, element, delimeter) - } - } - - return tokenArray -} diff --git a/internal/config/config.go b/internal/config/config.go deleted file mode 100644 index ad35ee7..0000000 --- a/internal/config/config.go +++ /dev/null @@ -1,56 +0,0 @@ -package config - -import ( - "errors" - "fmt" - "os" - "path/filepath" - - "gopkg.in/yaml.v3" -) - -type Config struct { - Filters []string `yaml:"filters"` - Iterations int `yaml:"iterations"` -} - -func getDefaultConfigPath() (defaultPath string, err error) { - programName := "uptfs" - configFileName := "config.yaml" - - if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { - return filepath.Join(xdg, programName, configFileName), nil - } - - if home := os.Getenv("HOME"); home != "" { - return filepath.Join(home, programName, configFileName), nil - } - - return "", errors.New("both XDG_CONFIG_HOME and HOME are not set, can't proceed") -} - -func (c *Config) LoadConfig(filepath string) *Config { - if filepath == "" { - var err error - filepath, err = getDefaultConfigPath() - - if err != nil { - fmt.Printf("%v\n", err) - os.Exit(1) - } - } - - yamlFile, err := os.ReadFile(filepath) - - if err != nil { - fmt.Printf("%v\n", err) - os.Exit(1) - } - err = yaml.Unmarshal(yamlFile, c) - if err != nil { - fmt.Printf("%v\n", err) - os.Exit(1) - } - - return c -} diff --git a/internal/split/split.go b/internal/split/split.go deleted file mode 100644 index 65e1a14..0000000 --- a/internal/split/split.go +++ /dev/null @@ -1,44 +0,0 @@ -package split - -import ( - "strings" -) - -func checkArrayElementsEmpty(array []string) bool { - for _, str := range array { - if str != "" { - return false - } - } - return true -} - -func NewArrayWithSplit(initialArray []string, index int, token string, delimeter string) (result []string) { - split := strings.Split(token, delimeter) - splitLength := len(split) - - /* - When a token only consists of delimeter * N (N >= 0), - the resulting split consists of N empty elements. - Here we check if it is so and essentialy remove that token - from resulting array. - */ - splitIsEmpty := checkArrayElementsEmpty(split) - if splitIsEmpty { - result = append(initialArray[:index], initialArray[index+1:]...) - return result - } - - if splitLength > 1 { - if split[splitLength-1] == "" { - split = split[:splitLength-1] - } - - result = append(initialArray[:index], append(split, initialArray[index+1:]...)...) - } - if splitLength == 1 { - result = initialArray - } - - return result -} diff --git a/main.go b/main.go new file mode 100644 index 0000000..10c5c9a --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "uptfs/cmd" + +func main() { + cmd.Run() +} diff --git a/util/config/config.go b/util/config/config.go new file mode 100644 index 0000000..ad35ee7 --- /dev/null +++ b/util/config/config.go @@ -0,0 +1,56 @@ +package config + +import ( + "errors" + "fmt" + "os" + "path/filepath" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Filters []string `yaml:"filters"` + Iterations int `yaml:"iterations"` +} + +func getDefaultConfigPath() (defaultPath string, err error) { + programName := "uptfs" + configFileName := "config.yaml" + + if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" { + return filepath.Join(xdg, programName, configFileName), nil + } + + if home := os.Getenv("HOME"); home != "" { + return filepath.Join(home, programName, configFileName), nil + } + + return "", errors.New("both XDG_CONFIG_HOME and HOME are not set, can't proceed") +} + +func (c *Config) LoadConfig(filepath string) *Config { + if filepath == "" { + var err error + filepath, err = getDefaultConfigPath() + + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } + } + + yamlFile, err := os.ReadFile(filepath) + + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } + err = yaml.Unmarshal(yamlFile, c) + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } + + return c +} diff --git a/util/split/split.go b/util/split/split.go new file mode 100644 index 0000000..2fc29db --- /dev/null +++ b/util/split/split.go @@ -0,0 +1,45 @@ +package split + +import ( + "strings" +) + +func checkArrayElementsEmpty(array []string) bool { + for _, str := range array { + if str != "" { + return false + } + } + return true +} + +func NewArrayWithSplit(initialArray []string, index int, token string, delimeter string) (result []string) { + split := strings.Split(token, delimeter) + splitLength := len(split) + + /* + When a token only consists of delimeter * N (N >= 0), + the resulting split consists of N empty elements. + Here we check if it is so and essentialy remove that token + from resulting array. + */ + + splitIsEmpty := checkArrayElementsEmpty(split) + if splitIsEmpty { + result = append(initialArray[:index], initialArray[index+1:]...) + return result + } + + if splitLength > 1 { + if split[splitLength-1] == "" { + split = split[:splitLength-1] + } + + result = append(initialArray[:index], append(split, initialArray[index+1:]...)...) + } + if splitLength == 1 { + result = initialArray + } + + return result +} -- cgit v1.2.3