From 3b37b18581900d77cf2250c9e513d9c1c2e54b9e Mon Sep 17 00:00:00 2001 From: eeckert Date: Mon, 15 Jan 2024 10:28:38 -0700 Subject: [PATCH] possible fix for third project section. --- main/main.go | 100 +++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 47 deletions(-) diff --git a/main/main.go b/main/main.go index 4605e06..b0d47ed 100644 --- a/main/main.go +++ b/main/main.go @@ -34,12 +34,15 @@ func MPE_IsProject(input *Payload) (projectnumber string, project_name string) { separated_path := strings.Split(split_path, "/") //turn this into an array (slice?) to parse var sb strings.Builder // iterate through the path_data map. We want to find all strings that start with a number. - for _, value := range separated_path { + for keyvalue, value := range separated_path { matched := project_regex.MatchString(value) - if matched { + // Possible Fix for Project 3rd Number ID. + ProjectSection := keyvalue == 2|3|4 // /shared<0>/N-Data<1>/<2>/<3>/<4>/<5>/<6> + + if matched && ProjectSection { // if we match the Regex above, write values to SB. This will give us a string like "73|10 Office Of Somone|153 HelpMe|002 Phase 2" - sb.WriteString(value + "|") + sb.WriteString(value + "|") // add pipe separator } else { // DoNothing @@ -49,61 +52,64 @@ func MPE_IsProject(input *Payload) (projectnumber string, project_name string) { // now to assemble project number and project name from the resulting values... project_text := sb.String() project_slice := strings.Split(project_text, "|") - // first value should be a digit, if not this can't be a project - isProject0, err := regexp.MatchString(`(^[0-9])`, project_slice[0]) - if err != nil { - fmt.Printf("err.Error(): %v\n", err.Error()) - } - isProject1, err := regexp.MatchString(`(^[0-9])`, project_slice[1]) - if err != nil { - fmt.Printf("err.Error(): %v\n", err.Error()) - } - isProject2, err := regexp.MatchString(`(^[0-9])`, project_slice[2]) - if err != nil { - fmt.Printf("err.Error(): %v\n", err.Error()) - } - // isProject, _ = regexp.MatchString(`(^[0-9])`, project_slice[3]) - if isProject0 && isProject1 && isProject2 { + if strings.Count(project_text, "|") > 2 { // if more than 2 pipe | chars in string, could be a project. + // first value should be a digit, if not this can't be a project + isProject0, err := regexp.MatchString(`(^[0-9])`, project_slice[0]) + if err != nil { + fmt.Printf("err.Error(): %v\n", err.Error()) + } + isProject1, err := regexp.MatchString(`(^[0-9])`, project_slice[1]) + if err != nil { + fmt.Printf("err.Error(): %v\n", err.Error()) + } + isProject2, err := regexp.MatchString(`(^[0-9])`, project_slice[2]) + if err != nil { + fmt.Printf("err.Error(): %v\n", err.Error()) + } + // isProject, _ = regexp.MatchString(`(^[0-9])`, project_slice[3]) + if isProject0 && isProject1 && isProject2 { - project_regex, _ := regexp.Compile(`(^[0-9]{1,3})`) // regex to match against - regex_tidy_string, _ := regexp.Compile(`[-_]+|^\s`) //regex to tidy up folder paths - client_section := regex_tidy_string.ReplaceAllString(project_slice[1], "") // remove fancy chars from Client Name - project_section := regex_tidy_string.ReplaceAllString(project_regex.ReplaceAllString(project_slice[2], ""), " ") // remove leading-digits - project_section_digits := strings.Split(regex_tidy_string.ReplaceAllString(project_slice[2], " "), " ") //split on space for MOST file folders... + project_regex, _ := regexp.Compile(`(^[0-9]{1,3})`) // regex to match against + regex_tidy_string, _ := regexp.Compile(`[-_]+|^\s`) //regex to tidy up folder paths + client_section := regex_tidy_string.ReplaceAllString(project_slice[1], "") // remove fancy chars from Client Name + project_section := regex_tidy_string.ReplaceAllString(project_regex.ReplaceAllString(project_slice[2], ""), " ") // remove leading-digits + project_section_digits := strings.Split(regex_tidy_string.ReplaceAllString(project_slice[2], " "), " ") //split on space for MOST file folders... - sb.Reset() // reset string builder so I can use it again! - sb.WriteString(project_slice[0]) // 73 - sb.WriteString(strings.Split(client_section, " ")[0] + "-") // 7310 -- Client Name - sb.WriteString(project_section_digits[0]) // 7310-10 + sb.Reset() // reset string builder so I can use it again! + sb.WriteString(project_slice[0]) // 73 + sb.WriteString(strings.Split(client_section, " ")[0] + "-") // 7310 -- Client Name + sb.WriteString(project_section_digits[0]) // 7310-10 - // do we have a third section of the project number? - project_third := strings.Split(project_slice[3], " ")[0] - project_third_match, _ := regexp.MatchString(`(^[0-9]{1,3})`, project_third) - /* TODO: Better identification of the third number. - Currently seeing paths such as "/shared/n-data/12/34 test/45 ProjectName/deliverables/05 geotech" generating project numbers of 1234-45-05, - where it should be 1234-45 + // do we have a third section of the project number? + project_third := strings.Split(project_slice[3], " ")[0] + project_third_match, _ := regexp.MatchString(`(^[0-9]{1,3})`, project_third) + /* TODO: Better identification of the third number. + Currently seeing paths such as "/shared/n-data/12/34 test/45 ProjectName/deliverables/05 geotech" generating project numbers of 1234-45-05, + where it should be 1234-45 - This is due to the RegEx Match looking for all folders that start with digits, and not being concerned about where they are in the string. + This is due to the RegEx Match looking for all folders that start with digits, and not being concerned about where they are in the string. - */ - if project_third_match { - // if we have a third section, then add another section to the project number, and append to the project name - third_project_name := project_regex.ReplaceAllString(project_slice[3], "") // 7310-10-153 - project_name = project_section + "-" + regex_tidy_string.ReplaceAllString(third_project_name, "") // remove project digits from beginning of string and append - project_third = regex_tidy_string.ReplaceAllString(project_third, " ") - sb.WriteString("-" + project_third) + */ + if project_third_match { + // if we have a third section, then add another section to the project number, and append to the project name + third_project_name := project_regex.ReplaceAllString(project_slice[3], "") // 7310-10-153 + project_name = project_section + "-" + regex_tidy_string.ReplaceAllString(third_project_name, "") // remove project digits from beginning of string and append + project_third = regex_tidy_string.ReplaceAllString(project_third, " ") + sb.WriteString("-" + project_third) + + } else { + project_name = project_section // remove project digits from beginning of string + + } + + projectnumber = sb.String() // I should now have a project number! } else { - project_name = project_section // remove project digits from beginning of string + return "NotAProject", "NoPath" } - projectnumber = sb.String() // I should now have a project number! - - } else { - return "NotAProject", "NoPath" - } return