Skip to content

Commit

Permalink
Improve dictionary
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Tiwari <[email protected]>
  • Loading branch information
Revolyssup committed Nov 11, 2022
1 parent f42d003 commit 4118001
Showing 1 changed file with 55 additions and 13 deletions.
68 changes: 55 additions & 13 deletions utils/manifests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,51 @@ func deleteFile(path string) error {
return nil
}

//TODO: After finalizing on where to keep dictionary.json, use that file to initialize dict map in init()
// var dict = map[string]string{}

// func init() {
// dictf, err := os.Open("./dictionary.json")
// if err != nil {
// return
// }
// byt, err := io.ReadAll(dictf)
// if err != nil {
// return
// }

// err = json.Unmarshal(byt, &dict)
// if err != nil {
// return
// }
// }

// This helps in formating the leftover fields using a pre-defined dictionary
func useDictionary(input string) string {
dict := map[string]string{ // includes Whitelist words
"Mesh Sync": "MeshSync",
"additional Properties": "additionalProperties", //This field is used by RJSF and should not include white space
"ca Bundle": "CA Bundle",
"mtls": "mTLS",
"m TLS": "mTLS",
}
for comp := range dict {
if comp == input {
return dict[comp]
// If dictionary returns true then any other formatting should be skipped
// The key in the dictionary contains completely unedited fields that are expected in certain input files
// The value is then the in-place replacement for those specific words
func useDictionary(input string, invert bool) (string, bool) {
dict := map[string]string{
"MeshSync": "MeshSync",
"additionalProperties": "additionalProperties",
"caBundle": "CA Bundle",
"mtls": "mTLS",
"mTLS": "mTLS",
}
for comp, val := range dict {
if invert {
if val == input {
fmt.Println("inverted ", val, " to ", comp)
return comp, true
}
} else {
if comp == input {
return val, true
}
}

}
return input
return input, false
}

// While going from Capital letter to small, insert a whitespace before the capital letter.
Expand All @@ -221,6 +251,10 @@ func FormatToReadableString(input string) string {
if len(input) == 0 {
return ""
}
input, found := useDictionary(input, false)
if found {
return input
}
finalWord := string(input[0])
for i := range input {
if i == 0 {
Expand All @@ -238,7 +272,15 @@ func FormatToReadableString(input string) string {
finalWord += " " + string(input[i])
}
}
return useDictionary(strings.Join(strings.Fields(strings.TrimSpace(finalWord+input[len(input)-1:])), " "))
return strings.Join(strings.Fields(strings.TrimSpace(finalWord+input[len(input)-1:])), " ")
}

func DeFormatReadableString(input string) string {
output, found := useDictionary(input, true)
if found {
return output
}
return strings.ReplaceAll(output, " ", "")
}

const (
Expand Down

0 comments on commit 4118001

Please sign in to comment.