go-json-rest icon indicating copy to clipboard operation
go-json-rest copied to clipboard

Add support for casting environment variables to standard types.

Open ruseinov opened this issue 9 years ago • 4 comments

What could be done is the Env map could be turned to a type and some convenience methods could be added for type casting, to avoid doing .(int) etc.

Something like Env.Int("param_name") or similar.

ruseinov avatar Dec 11 '15 17:12 ruseinov

What would be the benefit over type assertions (eg: .(int)) ? This is the same amount of typing, and type assertions seem more Go-ish.

ant0ine avatar Jan 18 '16 23:01 ant0ine

It's just nicer this way, you get autocomplete for some common types, also ForceType is also nice to have for convenience.

I've stolen this idea from https://godoc.org/github.com/garyburd/redigo/redis#hdr-Reply_Helpers , feel free to browse through it.

ruseinov avatar Jan 19 '16 10:01 ruseinov

I thing the benefit is caller does not assert type itself. Maybe the method can be assert and return zero value when Env cannot assert.

wingyplus avatar Jan 20 '16 10:01 wingyplus

Exactly!

On Wed, Jan 20, 2016 at 11:36 AM Thanabodee Charoenpiriyakij < [email protected]> wrote:

I thing the benefit is caller does not assert type itself. Maybe the method can be assert and return zero value when Env cannot assert.

— Reply to this email directly or view it on GitHub https://github.com/ant0ine/go-json-rest/issues/168#issuecomment-173165798 .

ruseinov avatar Jan 20 '16 10:01 ruseinov