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 IfFunc helper #99

Closed
wants to merge 3 commits into from
Closed

Add IfFunc helper #99

wants to merge 3 commits into from

Conversation

markuswustenberg
Copy link
Member

@markuswustenberg markuswustenberg commented May 25, 2022

IfFunc is like If, except it takes a callback function that returns a Node instead of a Node directly.
Great when the condition is a nil check and the function uses the value that's not nil.

@codecov
Copy link

codecov bot commented May 25, 2022

Codecov Report

Merging #99 (43239e8) into master (c868c52) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master       #99   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          491       496    +5     
=========================================
+ Hits           491       496    +5     
Impacted Files Coverage Δ
gomponents.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c868c52...43239e8. Read the comment docs.

@markuswustenberg
Copy link
Member Author

@oderwat @averageflow if you're still interested in this project and have some spare time, I would love to hear your thoughts on this little change. 😊

@oderwat
Copy link
Contributor

oderwat commented May 25, 2022

It is cool but I am not sure if it is so much better than a simple if/else. That is because you always have to type the function definition in addition to the construct itself. As this is needed you also can simply use it as the inline function.

But with go-app (which I actually use now) the "app.If()" is much worse than your idea and I thought about having something similar to your version with go-app but then I ended up just inlining the if. See maxence-charriere/go-app#632

Sadly this is not possible with your package because the anonymous function does not satisfy the interface. I just leave this here

package main

import (
	g "github.com/maragudk/gomponents"
	"os"
	"testing"
)

func IfFunc(condition bool, cb func() g.Node) g.Node {
	if condition {
		return cb()
	}
	return nil
}

// N is needed to implement the interface for anonymous functions
func N(cb func() g.Node) g.Node {
	// delegate to the anonymous function
	return cb()
}

func IfTestA(t *testing.T) {
	var message *string
	e := g.El("div",
		IfFunc(message != nil, func() g.Node {
			return g.El("span", g.Text(*message))
		}),
	)
	_ = e.Render(os.Stdout)
}

func IfTestB(t *testing.T) {
	var messagePtr *string
	e := g.El("div", N(func() (r g.Node) {
		if messagePtr != nil {
			r = g.El("span", g.Text(*messagePtr))
		}
		return
	}))
	_ = e.Render(os.Stdout)
}

P.S.: Sadly there also is no way to create a func NotNil[T any](T, func(T) g.Node) g.Node with generics as there is no a == T(nil).

markuswustenberg added a commit that referenced this pull request May 25, 2022
`Func` is a function that returns a `Node` that is itself also a `Node`.

Used with `If`, it enables lazy evaluation of the node to return, depending on the condition passed to `If`.

See also #99 for a different approach explored.
@markuswustenberg markuswustenberg mentioned this pull request May 25, 2022
@markuswustenberg
Copy link
Member Author

@oderwat thank you for your detailed reply! You're very kind. 😊

Your N function gave me a different idea: to create a function type that is also a Node. I'm exploring that in #101. That way, there's no choice between If/IfFunc, but as you also mention, it's still slightly verbose. Basically, it's a tradeoff between a wrapper type or having double if conditionals.

Any additional thoughts?

@teutonicjoe
Copy link
Contributor

I have moved on from my previous position, and don't work withGo, or with frontends anymore, but @dadobos does.

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.

None yet

3 participants