#280

Scala SourceTemplateSource read twice, second read is empty

    • Created on: Tue, Jan 31 (4 months ago)
    • Reported by: rocketraman
    • Assigned to: -
    • Milestone: -
    • Estimate: None/Small/Medium/Large None
    • Status: New
    • Priority: Normal (3)
    • Component: scalate-core
    • Permission type: Public
    The TemplateEngine.compileAndLoad() method calls the text() method of TemplateSource twice. The first call is val text = source.text (which is always called but used only by the pipeline), and the second call is on the generator generate() method which is passed the TemplateSource object. This is inefficient but it works when using an underlying File Object (as is the default situation), but when using a SourceTemplateSource to provide the template, this fails since a Scala Source can only be read once, after which it must be reset (and in some cases reset does not work either, for example if the Source is based on a stream e.g. Source.fromInputStream(...), unless the caller provides a reset function using the withReset method).

    Some possible fixes / improvements:

    1) Do source.reset on every call to text. This may require the caller to provide a resetFunction. The read will continue to be executed at least twice.

    2) Move call to source.text into the match for pipeline, so that source is only read once unless a pipeline is used, in which case it will continue to be executed twice (still requires fix #1 above).

    3) Cache the call to the text method in Resource and return the cached value if it has already been called before.

    4) Ensure the code never calls source.text twice.

    I have submitted https://github.com/scalate/scalate/pull/18 to implement a work-around via #1 and #2 but I suspect you may want to implement the optimal fix #3 or #4.
  • Followers
     
    Ico-users rocketraman 
     
    Attachments
    No attachments
    Associations
     
    No associations
    Activity
    Time Expenditure
    Loading