This depends on reflect. What's the speed penalty?
Go-Restructure: Sane regular expressions with struct fields
41–47 of 47 posts
Re: Go-Restructure: Sane regular expressions with struct fields
#42This is such a simple but beautiful idea. I love it! As a Swift developer I am really envious of these tagged fields on Go and how they simplify e.g. JSON parsing. And now I realise they can simplify regexp parsing as well. Dam!!
Re: Go-Restructure: Sane regular expressions with struct fields
#43Here's a 1-1 port in JavaScript https://github.com/benjamingr/js-restructure function matcher(obj) { "use strict"; let props = Object.getOwnPropertyNames(obj); const re = new RegExp(props.reduce((p, c) => p + (c.startsWith("_") ? obj[c] : `(${obj[c]})`), "")); props = props.filter(x => !x.startsWith("_")); return function(pattern) { let o = {}; const res = re.exec(pattern); for(let i = 0; i And example usage: ```js m…
JavaScript doesn't guarantee the order of keys. [edit] Also, the API makes less sense because JavaScript isn't statically typed so you can make up the result object on the fly. match('^(? \w+)@(? [^@]+)$', 'joe@example.com') => {'user': 'joe', 'host': 'example.com'}
But now you need to parse the string and correctly extract named groups before flogging that to the regex engine, whereas the other way around you do some string concatenation then match back on the groups by index.
Re: Go-Restructure: Sane regular expressions with struct fields
#44Here's a 1-1 port in JavaScript https://github.com/benjamingr/js-restructure function matcher(obj) { "use strict"; let props = Object.getOwnPropertyNames(obj); const re = new RegExp(props.reduce((p, c) => p + (c.startsWith("_") ? obj[c] : `(${obj[c]})`), "")); props = props.filter(x => !x.startsWith("_")); return function(pattern) { let o = {}; const res = re.exec(pattern); for(let i = 0; i And example usage: ```js m…
JavaScript doesn't guarantee the order of keys. [edit] Also, the API makes less sense because JavaScript isn't statically typed so you can make up the result object on the fly. match('^(? \w+)@(? [^@]+)$', 'joe@example.com') => {'user': 'joe', 'host': 'example.com'}
Re: Go-Restructure: Sane regular expressions with struct fields
#45Neat. It could adopt the key:"value" convention of other struct tag libraries (e.g. json:"name" xml:"name") to make it more composable.
Re: Go-Restructure: Sane regular expressions with struct fields
#46Very nice solution! I try to avoid using 3rd party libraries but this I would use without hesitation. Some benchmarks would be interesting.
Re: Go-Restructure: Sane regular expressions with struct fields
#47I recommend that the first example be changed to use struct{} as well. The "strings" are still present in the struct footprint if you leave them there: http://play.golang.org/p/ZG1ULgzSwZ And people luuuuuv to pick up the one example where you did something a bit wrong and copy paste like mad.... I'd really rather see something that generates marshaling code based on a regex or something, though. This loads an awful…
One alternative is to build up regexes dynamically concatenating smaller regexes, but that leads to whole other kind of hell.