Skip to content

Conversation

@megoth
Copy link
Contributor

@megoth megoth commented Feb 19, 2019

Should fix #1104

Copy link
Member

@kjetilk kjetilk left a comment

Choose a reason for hiding this comment

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

Looks good.

I'm thinking that it effectively does a greedy match, and I'm thinking it should be more modest... :-) In terms of a regexp, it did ^/index\..*/, and with this patch ^/index\..*[^\.acl]$/, but an alternative solution to this problem would have been ^/index.(.*?)/. In the latter case, it would have matched only one period after the filename, and so excluded the .acl, since that would have had two .s.

Not sure what would have been the best solution, though, so Approve.

Copy link
Contributor

@rubensworks rubensworks left a comment

Choose a reason for hiding this comment

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

I would propose to not hardcode the .acl suffix, as this would result in the same problem for .meta files. Another solution may be to configure an array for all blacklisted suffixes in the constructor.

Copy link
Member

@kjetilk kjetilk left a comment

Choose a reason for hiding this comment

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

Good point, @rubensworks . Yeah, I can see it being passed around as suffixAcl here and there. Doesn't seem to be very consistently applied, but I think it is a good idea to look into using that.

@megoth
Copy link
Contributor Author

megoth commented Feb 20, 2019

I'll push an update soon that doesn't hardcode it ^_^

@megoth
Copy link
Contributor Author

megoth commented Feb 20, 2019

@rubensworks @kjetilk I've pushed an update, hope it looks better ^_^ (Was uncertain how to include the knowledge of suffixes into resourceMapper, so hope the way I did it looks ok)

Copy link
Member

@kjetilk kjetilk left a comment

Choose a reason for hiding this comment

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

Yup, it does! @rubensworks ?


// Find a file with the same name (minus the dollar extension)
return (files.find(f => this._removeDollarExtension(f) === filename ||
(isIndex && f.startsWith(this._indexFilename + '.') && !f.endsWith(this._suffixes.acl))))
Copy link
Contributor

Choose a reason for hiding this comment

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

I would go one step further, and loop over all this._suffixes to exclude all file names that end with any of the defined suffixes. Right now, only index.html.acl will be blacklisted, but index.html.meta will still cause the same problem. A loop over this._suffixes would solve this.

For this purpose, it may be easier to refactor this._suffixes to be an array instead of a hash, as this is (AFAICS) only being used here.

@megoth
Copy link
Contributor Author

megoth commented Feb 28, 2019

@rubensworks then I've implemented your suggested changes (changing fileSuffixes from an object to an array). Could you have a look at the latest change, and see if you think it's good?

Copy link
Contributor

@rubensworks rubensworks left a comment

Choose a reason for hiding this comment

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

Looks good to me now! :-)

Perhaps the fileSuffixes variable name can be changed to something more descriptive such as blacklistedFileSuffixes, but I have no strong opinion on this.

Copy link
Contributor

@RubenVerborgh RubenVerborgh left a comment

Choose a reason for hiding this comment

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

I'm missing unit tests for ResourceMapper here, as well as in #1067 actually. It is one of the only unit-testable classes we have, and it used to have 100% coverage with many cases. Could we keep that up?

indexFilename = 'index',
overrideTypes = { acl: 'text/turtle', meta: 'text/turtle' }
overrideTypes = { acl: 'text/turtle', meta: 'text/turtle' },
fileSuffixes = ['.acl', '.meta']
Copy link
Contributor

Choose a reason for hiding this comment

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

Unclear variable name. I would name it something like controlExtensions.

this._defaultContentType = defaultContentType
this._indexFilename = indexFilename
this._types = { ...types, ...overrideTypes }
this._suffixes = fileSuffixes
Copy link
Contributor

Choose a reason for hiding this comment

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

Idem. They're not just suffixes, they are suffixes for a specific kinds of files that needs special behavior.

}
// Determine the path of an existing file
} else {
// Read all files in the corresponding folder
Copy link
Contributor

Choose a reason for hiding this comment

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

Outdated comment

// Find a file with the same name (minus the dollar extension)
let match = !searchIndex ? '' : (files.find(f => this._removeDollarExtension(f) === filename ||
(isIndex && f.startsWith(this._indexFilename + '.'))))
let match = searchIndex ? await this._getMatchingFile(folder, filename, isIndex) : ''
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if I would move this specific bit to a new function. Is there potential for reuse?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thinking was to make it more readable by giving it a name. Don't know of any potential reuse, no.

const files = await this._readdir(folder)

// Find a file with the same name (minus the dollar extension)
return (files.find(f => this._removeDollarExtension(f) === filename ||
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably expand this condition across multiple lines now, given the increased complexity.


// Find a file with the same name (minus the dollar extension)
return (files.find(f => this._removeDollarExtension(f) === filename ||
(isIndex && f.startsWith(this._indexFilename + '.') && !this._suffixes.some(suffix => f.endsWith(suffix)))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems wasteful to always iterate over the extensions.

How about, in the constructor, we instead use a regex to determine whether something is a control file? Then the default value is /.(?:acl|meta)$/, and this routine just does this._controlFile.test(f).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds reasonable - I'll write tests that make sure stuff works, and then try to optimize if per your suggestions 😸

@megoth
Copy link
Contributor Author

megoth commented Mar 3, 2019

Ah, yes, I've been lazy on tests here. I'll write them when I have time, hopefully during the next couple of days.

Trying to optimise code, and improve abstraction for better readability

Hopefully responding to the concerns of @RubenVerborgh: #1109 (comment) and #1109 (comment)
@megoth megoth requested a review from RubenVerborgh March 7, 2019 16:57
@megoth
Copy link
Contributor Author

megoth commented Mar 7, 2019

@RubenVerborgh think I have addressed your concerns; I'm uncertain of how to run coverage reports on this actually, could you check? (And perhaps drop a line on how you do it here? Could read up more on it, but am in bit of a hurry right now =P )

Copy link
Contributor

@RubenVerborgh RubenVerborgh left a comment

Choose a reason for hiding this comment

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

LGTM except for regexp.

this._defaultContentType = defaultContentType
this._indexFilename = indexFilename
this._types = { ...types, ...overrideTypes }
this._isControlFile = new RegExp(`.(?:${fileSuffixes.join('|')})$`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not the right regex: a) fileSuffixes already has a leading dot, so there are two dots now b) dots need to be escaped in regexes (but, since you are creating the regex from a string, actually needs double backslash '\\.).

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, you can just pass the regex itself instead of passing fileSuffixes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes it difficult to configure the suffixes though =/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hope the latest commit looks good.

@megoth megoth requested a review from RubenVerborgh March 7, 2019 18:19
@Ryuno-Ki
Copy link

Ryuno-Ki commented Mar 7, 2019

If you're concerned about coverage... why not use a tool like CodeCov?
You could make it even part of the conditions to flag a PR as mergeable.

this._defaultContentType = defaultContentType
this._indexFilename = indexFilename
this._types = { ...types, ...overrideTypes }
this._isControlFile = new RegExp(`(?:${fileSuffixes.map(fs => fs.replace('.', '\\.')).join('|')})$`)
Copy link
Contributor

Choose a reason for hiding this comment

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

fs.replace(/\./g, '\\.') strictly, but good enough 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regex is so difficult =P

@RubenVerborgh
Copy link
Contributor

If you're concerned about coverage... why not use a tool like CodeCov?

Coverage in general is not great yet; but it was for this particular class 😎

@kjetilk kjetilk merged commit 567b042 into release/v5.0.0 Mar 7, 2019
@kjetilk kjetilk deleted the fix/exclude-acl-in-url-mapping branch March 7, 2019 21:50
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.

6 participants