Back

Using falsy values for optional parameters

  • 0
  • General
Nevus
21 Apr, 2023, 18:20

gives an error in 1.3.1

TL;DR
The user is discussing the issue of using falsy values for optional parameters in Go. They mention that Go doesn't have a null type and suggest using 'nil' as an equivalent. They also mention that in Dart, empty values are removed. The user is trying to complete the Go SDK for a project and is facing difficulties with integer arguments. They mention that their current approach doesn't differentiate between a user passing zero as a default argument value or not wanting a default value of zero. Another user suggests using `Value.IsZero()` to detect empty values via reflection in Go. The conversation also touches on allowing empty strings as default values and the
Drake
21 Apr, 2023, 18:22

Interesting...I don't think it should throw an error...

Drake
21 Apr, 2023, 18:25

oh interesting..it does throw an error...

Drake
21 Apr, 2023, 18:27

it would probably be good to create an issue for this. i would like to allow empty strings, but we should get more feedback from users and the team

Drake
21 Apr, 2023, 18:44

btw, i don't think it's a problem with falsy values...it's just that we aren't allowing empty strings as default for string attributes

Nevus
21 Apr, 2023, 18:50

ahh. do you see any way around

I am using Go and without an SDK. This is relevant because Go doesn't allow bool(value) to filter out falsy value and I am stuck ๐Ÿ™‚ The only way I see is to write a switch statement for every single data type and iterate through params.

Drake
21 Apr, 2023, 18:52

I'm not quite sure why the falsy check is needed? what are you trying to do?

Nevus
21 Apr, 2023, 18:57

trying to filter out empty strings

Nevus
21 Apr, 2023, 18:57

it turns out get requests dont support it either

Drake
21 Apr, 2023, 18:58

Default != "" or was there something else you were trying to do?

Nevus
21 Apr, 2023, 19:01

I mean it works for string attributes but there's also int attribute and float attribute and enum attribute for database, query and search for list* endpoints

Nevus
21 Apr, 2023, 19:02

I'll just wait for github issue to see if y'all revert back to supporting empty values should I close this post?

Drake
21 Apr, 2023, 19:03

the github issue is going to be focused on allowing empty strings as default value when creating an attribute. I think your problem is kind of unrelated

Drake
21 Apr, 2023, 19:05

sorry i really don't understand what you're trying to do so it's hard to provide an adequate answer.

That said, there is a Value.IsZero() that might be helpful for you: https://stackoverflow.com/questions/13901819/quick-way-to-detect-empty-values-via-reflection-in-go

Nevus
21 Apr, 2023, 19:08

I am trying to complete the go sdk at https://github.com/appwrite/sdk-for-go Its painful because go doesn't have default values for function arguments. I just realized that with my current approach for integer arguments, there's no way to differentiate if the user passed zero because they dont want a default argument or because they want a default value of 0 ๐Ÿคฆโ€โ™‚๏ธ

Nevus
21 Apr, 2023, 19:11

Up until now I only considered string argument so if the user passed an empty argument (for example to search parameter of listCollection) it was safe to assume it could be excluded from the request

Drake
21 Apr, 2023, 19:21

you know we the SDK needs to be created by our SDK generator, right?

For integers, the API should allow 0 and null.

Drake
21 Apr, 2023, 19:22

as of 1.3, i think we handle 3 different cases distinctly:

  1. value provided
  2. null
  3. no value provided
Drake
21 Apr, 2023, 19:25

From the SDK perspective, the function should expect a nullable string and pass that along

Nevus
21 Apr, 2023, 19:25

The problem is still the same. My assumption was to have the user provide falsy values for optional arguments and filter it out in Client class when sending the request. This doesn't work for integers. I realized it doesn't work for integers during this chat.

Nevus
21 Apr, 2023, 19:27

Since Go needs a definite type for a function so it cant be string or null. Technically it can be a pointer type which allows nil but then the user has to do &"Hello" which is just annoying.

Drake
21 Apr, 2023, 19:27

heh...it looks like in dart, we remove values if they are null: https://github.com/appwrite/sdk-for-dart/blob/58120822b89791993a8a8a87ba2f6245555005df/lib/src/client_mixin.dart#L15

Maybe you should just do the same

Nevus
21 Apr, 2023, 19:35

Unfortunately go doesn't have null type. At least one that can be used instead of any type of variable

Drake
21 Apr, 2023, 20:47

The equivalent should be nil

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more