Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types for baton, flux, log.lua. Change rxi-json-lua to be more specific #43

Merged
merged 3 commits into from
Mar 9, 2023

Conversation

icy-lava
Copy link
Contributor

@icy-lava icy-lava commented Mar 5, 2023

Definitions for these libraries:

I changed json.lua to use more specific types rather than any.
You can still force it to accept any value like so:

local json = require 'json'

local t: table = {value = 'data'}

local encoded = json.encode(t as json.Value)
local decoded = json.decode(encoded) as any

@hishamhm
Copy link
Member

hishamhm commented Mar 6, 2023

Cool, thanks!

My 2 cents on the json changes:

I think updating the json decode definition should be fine, since the one with any requires as casts to use it in any form anyway and this new one might allow more useful behavior with is on the result.

I think for encode, any should be fine though? After all, most likely this library will be taking other table types, and casting as json.Value does not provide anything in terms of checking/validation, so ultimately it's up for json.encode to validate the input. So keeping the encode signature as is with any would make it more convenient, IMO.

@icy-lava
Copy link
Contributor Author

icy-lava commented Mar 7, 2023

Yeah that's fair. Another option would be to make the encode more lenient:

encode: function(value: number | boolean | string | table): string

That way simple simple errors like directly passing in a function or cdata/userdata would be caught, but tables containing invalid types would still be only caught at runtime.

Since this is not a common scenario I'm ok with making the encode input any though.

Also, now that I think about it, you have the option to use compile time validation yourself:

local obj: json.Object = {value = 'data'}
local encoded = json.encode(obj)

@hishamhm hishamhm merged commit ab11e3d into teal-language:master Mar 9, 2023
@hishamhm
Copy link
Member

hishamhm commented Mar 9, 2023

The table type is a bit wonky (it's an alias to {any:any}) so I think just keeping encode using any should be okay for now.

Thank you for the revision!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants