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 custom extension support for importing source file #350

Merged
merged 12 commits into from
Nov 13, 2021

Conversation

KEINOS
Copy link
Contributor

@KEINOS KEINOS commented Nov 10, 2021

This PR features issue #286 by implementing tengo.Compiler.SetImportFileExt() method which allows specifying custom extension names.

This feature aims for "tengo" as a library usage and should not affect the "/cmd/tengo" itself.

func main(){
...
	c := tengo.NewCompiler(srcFile, nil, nil, modules, nil)
	c.EnableFileImport(true)
	c.SetImportDir(filepath.Dir(pathFileSource))
+	err := c.SetImportFileExt(".tengo", ".mshk") // It will search *.tengo and *.mshk script files
...
}

  • EDIT: Previously, in the first commit, c.SetImportExt(".mshk") searched for both *.tengo and *.mshk (*.tengo by default) with no input validation.
    But currently it needs to explicitly define as c.SetImportFileExt(".tengo", ".mshk") for both file extensions. And returns an error if any given args are invalid as well.

@KEINOS KEINOS marked this pull request as ready for review November 10, 2021 14:26
@d5
Copy link
Owner

d5 commented Nov 10, 2021

Thanks for the PR. I will have to look at the code later tonight, but can we change the function signature to something like:

SetImportFileExt(...string)

and it should replace the entire array instead of appending.

compiler.go Outdated Show resolved Hide resolved
@KEINOS
Copy link
Contributor Author

KEINOS commented Nov 11, 2021

@d5
can we change the function signature

ok.

- func (c *Compiler) SetImportExt(ext string) {
+ func (c *Compiler) SetImportFileExt(exts ...string) {

it should replace the entire array instead of appending.

roger roger. ( ̄^ ̄)ゞ

@KEINOS KEINOS requested a review from geseq November 11, 2021 03:18
geseq
geseq previously approved these changes Nov 11, 2021
compiler.go Outdated Show resolved Hide resolved
@KEINOS KEINOS requested a review from d5 November 11, 2021 08:09
compiler_test.go Outdated Show resolved Hide resolved
compiler.go Show resolved Hide resolved
@KEINOS
Copy link
Contributor Author

KEINOS commented Nov 12, 2021

I wonder if we have to document it but couldn't determine where. Is the godoc enough?

@d5
Copy link
Owner

d5 commented Nov 12, 2021

we don't have documentation for that particular function, but maybe we can add a small note somewhere under here https://github.com/d5/tengo/blob/master/docs/tutorial.md#modules?

@KEINOS
Copy link
Contributor Author

KEINOS commented Nov 13, 2021

@d5
maybe we can add a small note somewhere under here

Done. How about it? Is it clear enough?

@geseq
Copy link
Collaborator

geseq commented Nov 13, 2021

LGTM. Looks much better overall

@d5 d5 merged commit 4846cf5 into d5:master Nov 13, 2021
@KEINOS KEINOS deleted the de-hardcode-extension branch November 14, 2021 01:41
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.

3 participants