Skip to content

Commit

Permalink
ref(proxy): create enum based path finer template paths (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 21, 2023
1 parent a195177 commit 2116eb8
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/app/proxy/path-finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ import (
"github.com/snivilised/extendio/xfs/nav"
)

type pfPath uint

const (
pfPathUndefined pfPath = iota
pfPathSetupInlineDestFolder
pfPathSetupInlineDestFileOriginalExt
)

const (
inlineDestinationTempl = ""
)

type (
templateSegments []string
pfTemplatesCollection map[string]templateSegments
pfTemplatesCollection map[pfPath]templateSegments
pfFieldValues map[string]string
)

Expand All @@ -26,13 +34,13 @@ func init() {
pfTemplates = pfTemplatesCollection{
// we probably have to come up with better key names...
//
"setup-inline-dest-folder": templateSegments{
pfPathSetupInlineDestFolder: templateSegments{
"${{OUTPUT-ROOT}}",
"${{ITEM-SUB-PATH}}",
"${{TRASH-LABEL}}",
},

"setup-inline-dest-file-original-ext": templateSegments{
pfPathSetupInlineDestFileOriginalExt: templateSegments{
"${{ITEM-NAME-ORIG-EXT}}",
},
}
Expand All @@ -54,11 +62,13 @@ func (tc pfTemplatesCollection) evaluate(
quantity = 1
)

return lo.Reduce(placeHolders, func(acc, field string, _ int) string {
result := lo.Reduce(placeHolders, func(acc, field string, _ int) string {
return strings.Replace(acc, field, values[field], quantity)
},
sourceTemplate,
)

return filepath.Clean(result)
}

// INLINE-MODE: EJECT | INLINE (should we call this a strategy?
Expand Down Expand Up @@ -220,28 +230,23 @@ func (f *PathFinder) Destination(info *destinationInfo) (destinationFolder, dest
)

destinationFolder = func() string {
templateFolderSegments := pfTemplates["setup-inline-dest-folder"]
templateFolderPath := pfTemplates.expand(filepath.Join(templateFolderSegments...))
folder := pfTemplates.evaluate(templateFolderPath, templateFolderSegments, pfFieldValues{
segments := pfTemplates[pfPathSetupInlineDestFolder]
path := pfTemplates.expand(filepath.Join(segments...))

return pfTemplates.evaluate(path, segments, pfFieldValues{
"${{OUTPUT-ROOT}}": to,
"${{ITEM-SUB-PATH}}": info.item.Extension.SubPath,
"${{TRASH-LABEL}}": trashLabel,
})
folder = filepath.Clean(folder)

return folder
}()

destinationFile = func() string {
templateFileSegments := pfTemplates["setup-inline-dest-file-original-ext"]
templateFilePath := pfTemplates.expand(filepath.Join(templateFileSegments...))
segments := pfTemplates[pfPathSetupInlineDestFileOriginalExt]
path := pfTemplates.expand(filepath.Join(segments...))

file := pfTemplates.evaluate(templateFilePath, templateFileSegments, pfFieldValues{
return pfTemplates.evaluate(path, segments, pfFieldValues{
"${{ITEM-NAME-ORIG-EXT}}": info.item.Extension.Name,
})
file = filepath.Clean(file)

return file
}()

return destinationFolder, destinationFile
Expand Down

0 comments on commit 2116eb8

Please sign in to comment.