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

Allow glob pattern to specify files #18

Open
bramp opened this issue Feb 7, 2024 · 4 comments
Open

Allow glob pattern to specify files #18

bramp opened this issue Feb 7, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@bramp
Copy link
Contributor

bramp commented Feb 7, 2024

Today I have to write

@EmbedStr('xml/basic.xml')
const basic = _$basic;

@EmbedStr('xml/river.xml')
const river = _$river;

...

const files = {
  'xml/basic.xml': basic,
  'xml/river.xml': river,
};

Instead I would love to write

@EmbedDirectory('xml')
const files = _$files;
@fujidaiti fujidaiti added the enhancement New feature or request label Feb 7, 2024
@fujidaiti
Copy link
Owner

fujidaiti commented Feb 7, 2024

API idea:

@EmbedStr('xml/*.xml') // A glob pattern
const files = _$files; // Map<String, String>

@EmbedBinary('images/*.png')
const images = _$images; // Map<String, List<int>>

// Generated code
const _$files = {
  'xml/basic.xml': "...",
  'xml/river.xml': "...",
};

const _$images = {
  'png/foo.png': [10, ...],
  'png/bar.png' : [8, ...],
}

@bramp
Copy link
Contributor Author

bramp commented Feb 7, 2024

Glob is a good idea.

I do wonder if you need a different annotation than the standard one, to perhaps avoid confusion / unexpected behaviour when you try and embed a single file that happens to include glob character.

@fujidaiti fujidaiti changed the title Support embedding all the files in a directory Allow glob pattern to specify files Feb 7, 2024
@fujidaiti
Copy link
Owner

fujidaiti commented Feb 7, 2024

I do wonder if you need a different annotation than the standard one, to perhaps avoid confusion / unexpected behaviour when you try and embed a single file that happens to include glob character.

Maybe we could avoid this problem by explicitly typing annotated variables depending on the expected output types. (EmbedLiteral already does almost the same thing.)

// Default output type depends on the result of glob search
@EmbedStr('xml/*.xml')
const basic = _$basic;

// Expects a single output
@EmbedStr('xml/basic.xml')
const String basic = _$basic;

// Expects multiple outputs
@EmbedStr('xml/*.xml')
const Map<String, String> files = _$files;

One good thing about this approach is that the Dart compiler would warn us if a file path unexpectedly contains a glob pattern.

// Compile error: Cannot assign a Map to a String variable
@EmbedStr('xml/*.xml')
const String basic = _$basic;

@bramp, what do you think? Is it still confusing?

@bramp
Copy link
Contributor Author

bramp commented Feb 7, 2024

Still could be error prone, but seems reasonable enough. I think my concern is a minor one, and I guess either way it'll hopefully be caught early.

I'm happy with whichever approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants