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

Interface for template engines #251

Merged
merged 4 commits into from
May 24, 2023

Conversation

mfiumara
Copy link
Contributor

Initial draft for having interfaces for template engines. This can reworked and is open for discussion.

applies to #248

@ReneWerner87
Copy link
Member

@mfiumara thanks for the effort
the interface looks good, but the usage in the first engine does not

in many cases the engines have additional functionalities, so i think we have to export them as well

for the check if the engine complies with the interface i would suggest we use "embedded interfaces" or "typechecks in test or real code".

@mfiumara
Copy link
Contributor Author

Hi,

Thanks for the feedback I'll come up with a refinement of this draft later tonight by embedding interfaces. I see what you mean and we're indeed not exporting all functions of the engine right now.

@mfiumara
Copy link
Contributor Author

Hey I've added an embedded interface for Pug such that all functions are exported now.

For the remainder of the engines I'd go through all of them in a similar fashion. Let me know if anything else is required before I go through with that. Perhaps I should move the function comments inside the interface definition? Or copy them over?

@mfiumara mfiumara force-pushed the refactor-to-interfaces-#248 branch from 068bddb to a7d4e29 Compare May 11, 2023 18:06
Copy link
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

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

I think internal dir is not good idea. Someome else may want to use this interface in their projects. It can be template.go in the project root like how it's on storage repo

@ReneWerner87
Copy link
Member

I think internal dir is not good idea. Someome else may want to use this interface in their projects. It can be template.go in the project root like how it's on storage repo

Yes, good point

@mfiumara
Copy link
Contributor Author

@efectn Moved to root into template.go.

@mfiumara
Copy link
Contributor Author

mfiumara commented May 11, 2023

On another note I see that a lot of the functions are practically the same for all Engines. If we could make some or all of the fields of the Engine struct public we could also embed the struct and define most of the methods once for all of the engines. It will save a lot of code and will be easier to add new engines.

Let me know if that's a good idea or not, it does mean the members will become public which perhaps is not what we want.

@efectn
Copy link
Member

efectn commented May 11, 2023

On another note I see that a lot of the functions are practically the same for all Engines. If we could make some or all of the fields of the Engine struct public we could also embed the struct and define most of the methods once for all of the engines. It will save a lot of code and will be easier to add new engines.

Let me know if that's a good idea or not, it does mean the members will become public which perhaps is not what we want.

Agree with you. It's worth to try

@mfiumara
Copy link
Contributor Author

mfiumara commented May 11, 2023

@efectn I've done the following:

  • Added struct definition into internal: this should not be used by external packages as far as I understand
  • The interface definition remains in the root (template.go) to be used by others
  • Individual engines can now embed the internal struct to have all basic methods promoted into their own interface type

I don't see the commit here yet in the PR, I'm not too familiar with Github but perhaps that takes some time to appear.

@mfiumara mfiumara marked this pull request as ready for review May 11, 2023 22:12
@mfiumara mfiumara marked this pull request as draft May 11, 2023 22:12
@mfiumara mfiumara force-pushed the refactor-to-interfaces-#248 branch from 17bbb27 to 791f0da Compare May 11, 2023 22:37
@mfiumara
Copy link
Contributor Author

Added second Engine Mustache to illustrate further. I should be able to do the rest fairly quickly let me know if this is the way forward.

@mfiumara mfiumara marked this pull request as ready for review May 13, 2023 14:56
@mfiumara
Copy link
Contributor Author

mfiumara commented May 13, 2023

I think I'm done @efectn. Would be great if you can have a look.

I additionally want to remove all redundant tests and move them to internal, that should save some unnecessary tests.

@mfiumara mfiumara changed the title wip: POC for adding interfaces to the templates Interface for template engines May 15, 2023
@efectn
Copy link
Member

efectn commented May 17, 2023

Can you check hound comments @mfiumara

@mfiumara
Copy link
Contributor Author

@efectn Done

@efectn
Copy link
Member

efectn commented May 20, 2023

@mfiumara can you resolve the conflicts, i removed parse() from engines

@mfiumara mfiumara force-pushed the refactor-to-interfaces-#248 branch 3 times, most recently from e99ec91 to f2c9833 Compare May 21, 2023 11:28
@mfiumara
Copy link
Contributor Author

mfiumara commented May 21, 2023

@efectn Fixed conflicts, rebased and squashed commits.

@mfiumara mfiumara requested a review from efectn May 21, 2023 11:34
@mfiumara mfiumara requested a review from efectn May 21, 2023 14:14

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
This commit introduces a common interface for template engines. Any new
template engines should adhere to the Engine interface in order to have
a common API.

The internal struct can be embedded to promote the default functions
into a specific template engine in order to reduce boilerplate. The
struct definition should only be used internally, whereas the interface
can be used externallly.

Fixes issue gofiber#248
@mfiumara mfiumara force-pushed the refactor-to-interfaces-#248 branch from 1eb6d6c to 77b204c Compare May 22, 2023 17:16
@mfiumara
Copy link
Contributor Author

Hi @efectn could this be merged? I had to resolve conflicts again due to other changes being merged.

@efectn
Copy link
Member

efectn commented May 22, 2023

Hi @efectn could this be merged? I had to resolve conflicts again due to other changes being merged.

The changes are OK to me but we should wait for one more review. @ReneWerner87 will review the PR when he is free.

@ReneWerner87
Copy link
Member

@mfiumara very good work

#251 (comment)

in many cases the engines have additional functionalities, so i think we have to export them as well

this fact is unfortunately not quite covered

try to explain it again
it can happen that an engine has additional methods beyond the basic functionality, which is totally ok
but currently they are not accessible, because you always return the general interface as return type of the "New" method
it's ok to return the original engine here, because internally it's ensured that it implements the methods of the interface and the users also have the possibility to pass the interface in their tests or methods

@ReneWerner87
Copy link
Member

By the way, if you made the last change, do we have to count up the major because there is a breaking change included here ?

@mfiumara
Copy link
Contributor Author

mfiumara commented May 23, 2023

Thanks for the feedback @ReneWerner87. At the moment there are no extra methods being exposed by any of the engines as far as I can tell, which is why I'm returning the same interface for all the engines and have removed the interfaces defined in the template engines.

If you want I can define the interface separately again for each template engine and return that in the Newxxx functions, but none of them will be implementing any additional template specific functionality.

Regarding backwards compatibility, I'm not 100% sure how or when this package breaks backwards compatible as I'm not aware how external packages depend on this one. I can bump the version but not sure how you go about this in this project, maybe it's best if I leave that up to you to do once any other changes are agreed upon.

- remove the internal folder, because the use is not possible from the outside
- replay the original engine to use engine specific code extensions
- renaming of core structs and interfaces
@ReneWerner87 ReneWerner87 merged commit cdd440a into gofiber:master May 24, 2023
@mfiumara mfiumara deleted the refactor-to-interfaces-#248 branch May 24, 2023 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants