-
-
Notifications
You must be signed in to change notification settings - Fork 52
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to replace this with:
Right now, we've got the risk that someone could do something like:
func TestF(t *testing.T) { var n Nullable[string] n.Null = true b, err := n.MarshalJSON() fmt.Printf("b: %v\n", b) fmt.Printf("b: %s\n", b) fmt.Printf("err: %v\n", err) b[1] = '_' b_, err := n.MarshalJSON() fmt.Printf("b: %v\n", b_) fmt.Printf("b: %s\n", b_) fmt.Printf("err: %v\n", err) }
Which outputs:
b: [110 117 108 108]
b: null
err: <nil>
b: [110 95 108 108]
b: n_ll
err: <nil>
Note that nullBytes from the package is changed.
We could alternatively perform a protective copy, or in this case, just return a literal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not grok this in the first read. Trying to understand it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! Makes sense now.. So basically some one can alter the nullByte. Good catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think this should be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On similar line -- now I am thinking we do not need this Get() method too.. Especially when I used in my own implementation -- I did not feel them need to use it especially when it returns 2 values, the code becomes little clumsy.
I think we should add these util functions as and when we need them. wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure if these are needed if we export the values from the struct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. Agreed.
- Not exporting the
SetandNullkeys is not a good idea as to create a Nullable type it is just easy to have them. - Now, since we have the fields exported the methods really does not do any useful stuff.
I was thinking on that and figured we should have something like the following:
// HasValue returns true if the value is provided in json func (t *Nullable[T]) HasValue() bool { return t.Set && !t.Null }
HasValue() is a good util to have as I felt the need to that while using this type to answer this question:
- Is the value provided in JSON as well as is not null.
If this util is not present then one will have to do:
if t.Set && !t.Null {// do something}
vs
if t.HasValue() {// do something}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise we're not passing in the correct type.
Shadowing here is fine (in my opinion - happy to chat if there are things to be aware of)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo.. thanks.
Will fix that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind adding newlines for readability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth replacing this and similar ones with:
So we're failing earlier
jamietanna
commented
Jan 3, 2024
Was the original implementation of the code adapted from https://github.com/guregu/null ? It looks like it but we've not declared so, as the licence would expect?
sonasingh46
commented
Jan 3, 2024
Was the original implementation of the code adapted from https://github.com/guregu/null ? It looks like it but we've not declared so, as the licence would expect?
Not really but definitely adapted the nullByte stuff from here. https://github.com/guregu/null/blob/master/string.go#L15
Also I looked at a lot of libs around doing the Nullable distinction and the basic way is kind of same.
So if we need to declare it or do anything needful, sure! Thanks for bringing this.
bfc1c43 to
cbea05b
Compare
nullable type (追記ここまで)
We have spun out a separate package, `oapi-codegen/nullable` as a step towards oapi-codegen/oapi-codegen#1039. Until we have implemented oapi-codegen#27, we cannot add an explicit type alias in this package, so we can at least add some tests to cover additional functionality and expectations that the package should have when interplaying with `oapi-codegen`. Co-authored-by: Sebastien Guilloux <sebastien.guilloux@elastic.co> Co-authored-by: Ashutosh Kumar <ashutosh.kumar@elastic.co>
cbea05b to
f26cbc7
Compare
Signed-off-by: Ashutosh Kumar <ashutosh.kumar@elastic.co>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these actually Idempotency tests? Or can we find a better name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the word idempotency can be simple removed. I will do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not re-use err?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛑 please revert this back, the ordering was changed on purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this, we're using Truef on purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline was added on purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Signed-off-by: Ashutosh Kumar <ashutosh.kumar@elastic.co>
ab5218f to
e6e916a
Compare
Uh oh!
There was an error while loading. Please reload this page.
We have spun out a separate package,
oapi-codegen/nullableas a steptowards oapi-codegen/oapi-codegen#1039.
Until we have implemented #27, we cannot add an explicit type alias in
this package, so we can at least add some tests to cover additional
functionality and expectations that the package should have when
interplaying with
oapi-codegen.Co-authored-by: Sebastien Guilloux sebastien.guilloux@elastic.co
Co-authored-by: Ashutosh Kumar ashutosh.kumar@elastic.co