From 5cc960527e7b3d094de9f0ebe0783f4c62345052 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Fri, 3 Jan 2020 00:28:54 -0500 Subject: [PATCH] lang: funcs: Differentiate between empty and nil values It would be good to differentiate between receiving an empty value or not having received a value yet. This is similar to the previous commit. --- lang/funcs/core/deploy/abspath_func.go | 14 +++++++------- lang/funcs/core/deploy/readfile_func.go | 13 +++++++------ lang/funcs/core/deploy/readfileabs_func.go | 13 +++++++------ lang/funcs/core/os/readfile_func.go | 11 ++++++----- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/lang/funcs/core/deploy/abspath_func.go b/lang/funcs/core/deploy/abspath_func.go index 490961c4..d160a66e 100644 --- a/lang/funcs/core/deploy/abspath_func.go +++ b/lang/funcs/core/deploy/abspath_func.go @@ -43,7 +43,7 @@ type AbsPathFunc struct { data *interfaces.FuncData last types.Value // last value received to use for diff - path string // the active path + path *string // the active path result *string // last calculated output closeChan chan struct{} @@ -110,10 +110,10 @@ func (obj *AbsPathFunc) Stream() error { path := input.Struct()[pathArg].Str() // TODO: add validation for absolute path? - if path == obj.path { + if obj.path != nil && *obj.path == path { continue // nothing changed } - obj.path = path + obj.path = &path p := strings.TrimSuffix(obj.data.Base, "/") if p == obj.data.Base { // didn't trim, so we fail @@ -122,13 +122,13 @@ func (obj *AbsPathFunc) Stream() error { } result := p - if obj.path == "" { + if *obj.path == "" { result += "/" // add the above trailing slash back - } else if !strings.HasPrefix(obj.path, "/") { - return fmt.Errorf("path was not absolute, got: `%s`", obj.path) + } else if !strings.HasPrefix(*obj.path, "/") { + return fmt.Errorf("path was not absolute, got: `%s`", *obj.path) //result += "/" // be forgiving ? } - result += obj.path + result += *obj.path if obj.result != nil && *obj.result == result { continue // result didn't change diff --git a/lang/funcs/core/deploy/readfile_func.go b/lang/funcs/core/deploy/readfile_func.go index 70dca5e8..ed1f5c27 100644 --- a/lang/funcs/core/deploy/readfile_func.go +++ b/lang/funcs/core/deploy/readfile_func.go @@ -40,7 +40,7 @@ type ReadFileFunc struct { data *interfaces.FuncData last types.Value // last value received to use for diff - filename string // the active filename + filename *string // the active filename result *string // last calculated output closeChan chan struct{} @@ -107,10 +107,11 @@ func (obj *ReadFileFunc) Stream() error { filename := input.Struct()["filename"].Str() // TODO: add validation for absolute path? - if filename == obj.filename { + // TODO: add check for empty string + if obj.filename != nil && *obj.filename == filename { continue // nothing changed } - obj.filename = filename + obj.filename = &filename p := strings.TrimSuffix(obj.data.Base, "/") if p == obj.data.Base { // didn't trim, so we fail @@ -119,11 +120,11 @@ func (obj *ReadFileFunc) Stream() error { } path := p - if !strings.HasPrefix(obj.filename, "/") { - return fmt.Errorf("filename was not absolute, got: `%s`", obj.filename) + if !strings.HasPrefix(*obj.filename, "/") { + return fmt.Errorf("filename was not absolute, got: `%s`", *obj.filename) //path += "/" // be forgiving ? } - path += obj.filename + path += *obj.filename fs, err := obj.init.World.Fs(obj.data.FsURI) // open the remote file system if err != nil { diff --git a/lang/funcs/core/deploy/readfileabs_func.go b/lang/funcs/core/deploy/readfileabs_func.go index 2ac736c3..7a1681a4 100644 --- a/lang/funcs/core/deploy/readfileabs_func.go +++ b/lang/funcs/core/deploy/readfileabs_func.go @@ -40,7 +40,7 @@ type ReadFileAbsFunc struct { data *interfaces.FuncData last types.Value // last value received to use for diff - filename string // the active filename + filename *string // the active filename result *string // last calculated output closeChan chan struct{} @@ -107,20 +107,21 @@ func (obj *ReadFileAbsFunc) Stream() error { filename := input.Struct()["filename"].Str() // TODO: add validation for absolute path? - if filename == obj.filename { + // TODO: add check for empty string + if obj.filename != nil && *obj.filename == filename { continue // nothing changed } - obj.filename = filename + obj.filename = &filename fs, err := obj.init.World.Fs(obj.data.FsURI) // open the remote file system if err != nil { return errwrap.Wrapf(err, "can't load code from file system `%s`", obj.data.FsURI) } - content, err := fs.ReadFile(obj.filename) // open the remote file system + content, err := fs.ReadFile(*obj.filename) // open the remote file system // We could use it directly, but it feels like less correct. - //content, err := obj.data.Fs.ReadFile(obj.filename) // open the remote file system + //content, err := obj.data.Fs.ReadFile(*obj.filename) // open the remote file system if err != nil { - return errwrap.Wrapf(err, "can't read file `%s`", obj.filename) + return errwrap.Wrapf(err, "can't read file `%s`", *obj.filename) } result := string(content) // convert to string diff --git a/lang/funcs/core/os/readfile_func.go b/lang/funcs/core/os/readfile_func.go index e08048de..53ab1384 100644 --- a/lang/funcs/core/os/readfile_func.go +++ b/lang/funcs/core/os/readfile_func.go @@ -41,7 +41,7 @@ type ReadFileFunc struct { init *interfaces.Init last types.Value // last value received to use for diff - filename string // the active filename + filename *string // the active filename recWatcher *recwatch.RecWatcher events chan error // internal events wg *sync.WaitGroup @@ -112,10 +112,11 @@ func (obj *ReadFileFunc) Stream() error { filename := input.Struct()["filename"].Str() // TODO: add validation for absolute path? - if filename == obj.filename { + // TODO: add check for empty string + if obj.filename != nil && *obj.filename == filename { continue // nothing changed } - obj.filename = filename + obj.filename = &filename if obj.recWatcher != nil { obj.recWatcher.Close() // close previous watcher @@ -123,7 +124,7 @@ func (obj *ReadFileFunc) Stream() error { } // create new watcher obj.recWatcher = &recwatch.RecWatcher{ - Path: obj.filename, + Path: *obj.filename, Recurse: false, Flags: recwatch.Flags{ // TODO: add Logf @@ -189,7 +190,7 @@ func (obj *ReadFileFunc) Stream() error { } // read file... - content, err := ioutil.ReadFile(obj.filename) + content, err := ioutil.ReadFile(*obj.filename) if err != nil { return errwrap.Wrapf(err, "error reading file") }