Skip to content

Conversation

@dborovcanin
Copy link
Collaborator

What type of PR is this?

This is a refactor.

What does this do?

This changes the Nullable parser naming and adds a factory method to return a valid nullable with value.

Which issue(s) does this PR fix/relate to?

There is no such issue.

Have you included tests for your changes?

Yes.

Did you document any new/modified feature?

N/A

Notes

N/A

@dborovcanin dborovcanin requested a review from a team as a code owner July 3, 2025 16:38
@codecov
Copy link

codecov bot commented Jul 3, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 12 lines in your changes missing coverage. Please review.

Project coverage is 24.83%. Comparing base (0a18a58) to head (9eacb42).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/nullable/value.go 0.00% 8 Missing ⚠️
groups/events/events.go 0.00% 3 Missing ⚠️
channels/postgres/channels.go 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2999      +/-   ##
==========================================
+ Coverage   23.69%   24.83%   +1.13%     
==========================================
  Files         360      124     -236     
  Lines       67764    36646   -31118     
==========================================
- Hits        16058     9100    -6958     
+ Misses      50922    27093   -23829     
+ Partials      784      453     -331     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines 16 to 25
Value T
}

// FromString[T any] represents a parser function. It is used to avoid
func New[T any](v T, set bool) Value[T] {
return Value[T]{
Set: set,
Value: v,
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about below approach ?

Suggested change
Value T
}
// FromString[T any] represents a parser function. It is used to avoid
func New[T any](v T, set bool) Value[T] {
return Value[T]{
Set: set,
Value: v,
}
}
type Nullable interface {
~string | ~int | ~uint
}
// Value type is used to represent difference betweeen an
// intentionally omitted value and default type value.
type Value[T Nullable] struct {
Set bool
Value T
}
func New[T Nullable](v T) Value[T] {
return NewNull[T].Set(v)
}
func NewNull[T Nullable]() Value[T] {
return Value[T]{}
}
// Set sets the value and marks it as set.
func (v *Value[T]) Set(val T) {
v.Set = true
v.Value = val
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If T is any we could not determine v == nil

func New[T](v T) Value[T] {
   if v == nil  { // x not possible 
   ...
}

If we have constraint like below, then it makes sure that pointer or interface can't be passed so New[T Nullable] function can set without checking for nil

type Nullable interface {
	~string | ~int | ~uint
}
func New[T Nullable](v T) Value[T] {
	return NewNull[T].Set(v)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a union, but let's keep the factory method signature as is - it's simple enough and works for all cases. Setting the value is a nice idea, but we'd have to make it non-exported and use getters and setter, which is not Go idiomatic, and also does not bring anything to the table unless we make both set and value non-exported. Then, we can enforce that Set always makes set true, but I'm not sure that's even desired behavior. New(value, bool) should be enough and it's also the most flexible.

}

// FromString[T any] represents a parser function. It is used to avoid
func New[T nullable](v T, set bool) Value[T] {
Copy link
Contributor

@arvindh123 arvindh123 Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have nullable, then value could not be null
example:
var a int = 0
Then the following is not true
New(a,false)
a is not null, I see Set is used for to determine any value is set or value is nil,
by default int goes to 0 which is not nil

So new function can be like New(a)

If some one need to have int with null, then we can provide NewNull[int]()
a := NewNull[int]()
or
var a Value[int] which defaults to null

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK to restrict null value, that's why we have the Set field. If Set is false, we don't care about value. If it's true, we take the value, even if default.

group.ID = generateUUID(t)

updatedDesc := nullable.Value[string]{Set: true, Value: updatedDescription}
updatedDesc := nullable.New(updatedDescription, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this function

Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
Signed-off-by: Dusan Borovcanin <[email protected]>
@dborovcanin dborovcanin merged commit be66701 into absmach:main Jul 4, 2025
5 of 6 checks passed
@dborovcanin dborovcanin deleted the change-parser branch July 4, 2025 15:34
nyagamunene pushed a commit to nyagamunene/supermq that referenced this pull request Jul 9, 2025
arvindh123 pushed a commit that referenced this pull request Jul 30, 2025
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