purpleidea / mgmt

Next generation distributed, event-driven, parallel config management!
https://purpleidea.com/tags/mgmtconfig/
GNU General Public License v3.0
3.67k stars 315 forks source link

Feature: Url Parser for mcl #606

Open dbakong opened 4 years ago

dbakong commented 4 years ago

''' It turns out that it is useful to "parse" strings that represent URL's. Golang makes this a bit easier because it has: https://golang.org/pkg/net/url/#Parse -- we'd like to do something similar. Here's the catch:

0) Look up "pure functions", "side effects", and "functional programming" and spend 30 min understanding the basics.

1) Design the "API" for whatever our URL parsing should look like. Should it be one function or many, and what is/are the signature(s) ? Explain this clearly in a new GH issue.

2) Have a really, properly comprehensive set of test cases for this. Remember to test absurd things, or null/empty cases. In particular, the Parse function in golang has some surprises, so make sure you catch these!

'''

Intro:

Mgmt has a language - mcl - and it currently doesn't have a function to parse urls.

Design:

The functionality would rely on Golang's default url parsing package - specifically url.Parse to allow mcl to parse a given url and return various elements of the url, including the scheme, host and path. The function would take the FQDN version of a url.

The tests for the patch would cover:

The signature of the function would be similar to the below: ParseUrl(input []types.Value) (types.Value, error) - the function would both accept and return string values. Some errors handled would include:

The mcl signature will be: func(a str) str

mcl example

import "urlParse"

$scheme = urlParse.scheme()

file "/tmp/mgmt/url" {
    state => $const.res.file.state.exists,
    content => template("The url scheme is: {{ urlParse_print .scheme }}\n", $scheme),
}

file "/tmp/mgmt/" {
    state => $const.res.file.state.exists,
}
purpleidea commented 4 years ago

@dbakong I was expecting to see the recommended mcl signature, and some mcl examples...

purpleidea commented 4 years ago

@dbakong The example doesn't match the signature, and the scheme is only one small part...

dbakong commented 4 years ago

@purpleidea - The example above was only meant to show a small example of the url parser in action. I'll add more examples as I implement the patch. I'll work on matching the signature to the current example.

frebib commented 3 years ago

https://golang.org/pkg/net/url/#Parse Could this be used along with the simple function api and the funcgen code? I'm not sure how that handles functions that return errors, though

purpleidea commented 3 years ago

@frebib Correct, this could use the simple function API since it's completely pure. As for errors, you'd need to decide how to represent that in mgmt. Either a isValidURI() bool to know if it would error, and for the Parse function, on error you empty string (likely my preference) or if you want to hard error and shutdown the language. (Probably not recommended.)

Some background: This was a design exercise for Donald because he was learning golang and mgmt. If you're blocked and you need this function, please have a go at it, it should be a short ~1-2 hour project or so.