Last active
September 17, 2016 19:56
-
-
Save DronRathore/4b958dacba89b1b19a8d7c2709e3649c to your computer and use it in GitHub Desktop.
Convert Go http routes to express-js fashion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "os" | |
import "text/tabwriter" | |
import "regexp" | |
import "fmt" | |
func main(){ | |
// silent the groups to eliminate redundant data in match | |
var r = compileRegex("/:name/:service/:id(?:[0-9]{0,3})") | |
var regex = r.FindStringSubmatch("/api/buy/123") | |
var params = make(map[string]string) | |
// to print beautifully | |
var writer = tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight|tabwriter.Debug) | |
for i, name := range r.SubexpNames() { | |
if i == 0 { | |
// first value is matched string itself | |
continue; | |
} | |
params[name] = regex[i] | |
fmt.Fprintln(writer, "param: \t"+ string(name)+"\tvalue:\t" + string(regex[i])) | |
} | |
writer.Flush() | |
} | |
func compileRegex(url string) *regexp.Regexp { | |
var i = 0 | |
var buffer = "/" | |
var regexStr = "^" | |
var endVariable = ">(?:[A-Za-z0-9\\-\\_\\$\\.\\+\\!\\*\\'\\(\\)\\,]+))" | |
if url[0] == '/' { | |
i++ | |
} | |
for i < len(url) { | |
if url[i] == '/' { | |
// attach the last parsed part and then move ahead | |
regexStr += buffer + "/" | |
buffer = "" | |
i++ | |
} else { | |
if url[i] == ':' && ( (i-1 >=0 && url[i-1] == '/') || (i-1 == -1)) { | |
// a variable found, lets read it | |
var tempbuffer = "(?P<" | |
var variableName = "" | |
var variableNameDone = false | |
var done = false | |
var hasRegex = false | |
var innerGroup = 0 | |
// lets branch in to look deeper | |
i++ | |
for done != true && i < len(url) { | |
if url[i] == '/' { | |
// are we done yet? have the name? | |
if variableName != "" { | |
// are all brackets closed? | |
if innerGroup == 0 { | |
if hasRegex == false { | |
// attach the constant endVariable for URL param | |
tempbuffer += endVariable | |
} | |
done = true | |
break | |
} | |
} | |
// something went wrong in during parsing. panic. | |
tempbuffer = "" | |
break; | |
} else if url[i] == '(' { | |
// group begin, have we saved the variable name? | |
if variableNameDone == false { | |
variableNameDone = true | |
tempbuffer += ">" // close variable name tag | |
hasRegex = true // this group has regex | |
} | |
tempbuffer += string(url[i]) | |
if url[i - 1] != '\\' { | |
innerGroup++ | |
} | |
} else if url[i] == ')' { | |
tempbuffer += string(url[i]) | |
if url[i - 1] != '\\' { | |
innerGroup-- | |
} | |
} else { | |
if variableNameDone == false { | |
variableName += string(url[i]) | |
} | |
tempbuffer += string(url[i]) | |
} | |
i++ | |
} | |
if tempbuffer != "" { | |
if hasRegex == false && done == false { | |
tempbuffer += endVariable | |
} else if hasRegex { | |
tempbuffer += ")" | |
} | |
buffer += tempbuffer | |
} else { | |
panic("Invalid Route regex") | |
} | |
} else { | |
buffer += string(url[i]) | |
i++ | |
} | |
} | |
} | |
if buffer != "" { | |
regexStr += buffer | |
} | |
return regexp.MustCompile(regexStr + "(?:[\\/]{0,1})$") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment