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

resources: Create a ResourceFinder interface #9063

Merged
merged 1 commit into from
Apr 5, 2022

Conversation

bep
Copy link
Member

@bep bep commented Oct 20, 2021

And make both .Resources and resources implement it.

Note that GetRemote is not covered by this interface, as that is only available as a global template function.

Fixes #8653

@bep bep force-pushed the feat/resource-finder branch 2 times, most recently from 0e38e16 to cca66a8 Compare October 30, 2021 08:11
@bep
Copy link
Member Author

bep commented Oct 30, 2021

So, this is the common interface:

// ResourceFinder provides methods to find Resources.
type ResourceFinder interface {

	// Get locates the Resource with the given name in the current context (e.g. in .Page.Resources).
	//
	// It returns nil if no Resource could found.
	Get(name interface{}) Resource

	// GetMatch finds the first Resource matching the given pattern, or nil if none found.
	//
	// See Match for a more complete explanation about the rules used.
	//
	// It returns nil if no Resource could found, panics if pattern is invalid.
	GetMatch(pattern interface{}) Resource

	// Match gets all resources matching the given base path prefix, e.g
	// "*.png" will match all png files. The "*" does not match path delimiters (/),
	// so if you organize your resources in sub-folders, you need to be explicit about it, e.g.:
	// "images/*.png". To match any PNG image anywhere in the bundle you can do "**.png", and
	// to match all PNG images below the images folder, use "images/**.jpg".
	//
	// The matching is case insensitive.
	//
	// Match matches by using a relative pathwith Unix style slashes (/) and no
	// leading slash, e.g. "images/logo.png".
	//
	// See https://github.com/gobwas/glob for the full rules set.
	//
	// It looks for files in the assets file system.
	//
	// See Match for a more complete explanation about the rules used.
	//
	// It returns nil if no Resources could found, panics if pattern is invalid.
	Match(pattern interface{}) Resources

	// ByType returns resources of a given resource type (e.g. "image").
	// It returns nil if no Resources could found, panics if tp is invalid.
	ByType(tp interface{}) Resources
}

@bep
Copy link
Member Author

bep commented Apr 4, 2022

@jmooring I totally forgot about this branch. I have now rebased it against master. Note the absence of GetRemote. I'm not sure how to best handle that. It may not make much sense to have a .Resources.GetRemote, but maybe we could/should add it for completeness?

// ResourceFinder provides methods to find Resources.
// Note that GetRemote (as found in resources.GetRemote) is
// not covered by this interface, as this is only available as a global template function.
type ResourceFinder interface {

	// Get locates the Resource with the given name in the current context (e.g. in .Page.Resources).
	//
	// It returns nil if no Resource could found, panics if name is invalid.
	Get(name any) Resource

	// GetMatch finds the first Resource matching the given pattern, or nil if none found.
	//
	// See Match for a more complete explanation about the rules used.
	//
	// It returns nil if no Resource could found, panics if pattern is invalid.
	GetMatch(pattern any) Resource

	// Match gets all resources matching the given base path prefix, e.g
	// "*.png" will match all png files. The "*" does not match path delimiters (/),
	// so if you organize your resources in sub-folders, you need to be explicit about it, e.g.:
	// "images/*.png". To match any PNG image anywhere in the bundle you can do "**.png", and
	// to match all PNG images below the images folder, use "images/**.jpg".
	//
	// The matching is case insensitive.
	//
	// Match matches by using a relative pathwith Unix style slashes (/) and no
	// leading slash, e.g. "images/logo.png".
	//
	// See https://github.com/gobwas/glob for the full rules set.
	//
	// See Match for a more complete explanation about the rules used.
	//
	// It returns nil if no Resources could found, panics if pattern is invalid.
	Match(pattern any) Resources

	// ByType returns resources of a given resource type (e.g. "image").
	// It returns nil if no Resources could found, panics if typ is invalid.
	ByType(typ any) Resources
}

@bep bep force-pushed the feat/resource-finder branch 2 times, most recently from aca7eee to 3345c36 Compare April 4, 2022 17:05
@jmooring
Copy link
Member

jmooring commented Apr 4, 2022

It may not make much sense to have a .Resources.GetRemote, but maybe we could/should add it for completeness?

I vote no. I updated the image processing docs recently, and the separation between page and global resources made sense, at least to me:

https://gohugo.io/content-management/image-processing/#image-resources

A global resource is a file:

  • Within the assets directory, or
  • Within any directory mounted to the assets directory, or
  • Located on a remote server accessible via http or https

@bep
Copy link
Member Author

bep commented Apr 4, 2022

@jmooring OK, looking at the above, I will take another take on the interface to get rid of the any type for the strings (we do have some different string types, but Go's new generics should provide a cleaner way).

And make both .Resources and resources implement it.

This gets us 2 new methods/functions, so you can now also do:

* .Resources.Get
* resources.ByType

Note that GetRemote is not covered by this interface, as that is only available as a global template function.

Fixes gohugoio#8653
@bep bep merged commit e58a540 into gohugoio:master Apr 5, 2022
@github-actions
Copy link

github-actions bot commented Apr 6, 2023

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants