Skip to content

Instantly share code, notes, and snippets.

@alaa-alawi
Created May 21, 2012 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alaa-alawi/2763794 to your computer and use it in GitHub Desktop.
Save alaa-alawi/2763794 to your computer and use it in GitHub Desktop.
(defun make-upload-filename ()
(etypecase *upload-filename-generator*
;; the old behaviour.
(null t)
;; the new behaviour.
((or symbol function)
(lambda (&rest args)
(let ((filename (apply *upload-filename-generator* args)))
(when *file-upload-hook*
(funcall *file-upload-hook* filename))
filename)))
(directory-pathname
(lambda (&rest args &key file-name)
(let ((filename (make-pathname :name file-name
:directory *upload-filename-generator*)))
(when *file-upload-hook*
(funcall *file-upload-hook* filename))
filename)))))
(deftype directory-pathname ()
'(and pathname (satisfies cl-fad:directory-pathname-p)))
@hanshuebner
Copy link

You asked for review: The clauses for a function designator and a directory pathname are almost identical. Please refactor so that no code duplication occurs. Also, note that all functionality that you add needs documentation.

Maybe you can come up with a better name for upload-filename-generator, now that it is can be a directory pathname.

The invocation of the upload-filename-generator function should have :allow-other-keys t - That actually makes sense.

Finally, not having the rest of the source before me, why can make-upload-filename return T? Could the NIL case for upload-filename-generator be handled at the call site?

-Hans

@alaa-alawi
Copy link
Author

alaa-alawi commented May 21, 2012 via email

@alaa-alawi
Copy link
Author

I'll look into other comments and include them in the fork.

Thanks for your time.

@hanshuebner
Copy link

hanshuebner commented May 21, 2012 via email

@alaa-alawi
Copy link
Author

alaa-alawi commented May 28, 2012 via email

@alaa-alawi
Copy link
Author

(defun make-upload-filename-generator ()
  (etypecase *upload-filename-generator* 
    ;; the old behaviour.
    (null (lambda (&rest args)
            (declare (ignore args))
            (funcall #'make-tmp-file-name)))
    ;; the new behaviour.
    ((or symbol function) 
     (lambda (&rest args)
       (let ((filename (apply *upload-filename-generator* args :allow-other-keys t)))
         (when *file-upload-hook*
           (funcall *file-upload-hook* filename))
         filename)))))

@hanshuebner
Copy link

This looks good - One more thing - The file-upload-hook invocation should be moved out of make-tmp-file-name and called from the function that is returned from make-upload-filename-generator, something like;

(let ((make-filename (ecase ...)))
(lambda (&rest args)
(let ((filename (funcall make-filename args)))
(when file-upload-hook
(funcall ..))
filename)))

i.e. don't spread the hacky file-upload-hook throughout the code if it can be localized to one place.

Thanks,
Hans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment