Skip to content

Commit

Permalink
simplified function to turn alias into full path to make it testable
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed Sep 18, 2019
1 parent f36970c commit a28c2f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
33 changes: 19 additions & 14 deletions evaluator/functions.go
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"crypto/rand"
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"math"
Expand Down Expand Up @@ -1574,11 +1575,26 @@ func sourceFn(tok token.Token, env *object.Environment, args ...object.Object) o
// require("file.abs")
var history = make(map[string]string)

type StringFn func(string) (string, error)
var packageAliases map[string]string
var packageAliasesLoaded bool

func requireFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
getAlias := Memoize(util.ReadAliasFromFile)
a, error := getAlias(args[0].Inspect())
if !packageAliasesLoaded {
a, err := ioutil.ReadFile("./packages.abs.json")

// We couldn't open the packages, file, possibly doesn't exists
// and the code shouldn't fail
if err == nil {
// Try to decode the packages file:
// if an error occurs we will simply
// ignore it
json.Unmarshal(a, &packageAliases)
}

packageAliasesLoaded = true
}

a, error := util.UnaliasPath(args[0].Inspect(), packageAliases)
if error != nil {
return newError(tok, "error resolving '%s': %s\n", args[0].Inspect(), error.Error())
}
Expand All @@ -1588,17 +1604,6 @@ func requireFn(tok token.Token, env *object.Environment, args ...object.Object)
return doSource(tok, e, file, args...)
}

func Memoize(fn StringFn) StringFn {
return func(str string) (string, error) {
if res, ok := history[str]; ok {
return res, nil
}
res, err := fn(str)
history[str] = res
return res, err
}
}

func doSource(tok token.Token, env *object.Environment, fileName string, args ...object.Object) object.Object {
err := validateArgs(tok, "source", args, 1, [][]string{{object.STRING_OBJ}})
if err != nil {
Expand Down
23 changes: 2 additions & 21 deletions util/util.go
@@ -1,8 +1,6 @@
package util

import (
"encoding/json"
"io/ioutil"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -104,26 +102,9 @@ func UniqueStrings(slice []string) []string {
return list
}

// ReadAliasFromFile translates a path alias
// UnaliasPath translates a path alias
// to the full path in the filesystem.
func ReadAliasFromFile(path string) (string, error) {
var packageAlias map[string]string
a, err := ioutil.ReadFile("./packages.abs.json")

// We couldn't open the packages, file, possibly doesn't exists
// and the code shouldn't fail
if err != nil {
return path, nil
}

// Try to decode the packages file:
// if an error occurs we will simply
// ignore it
err = json.Unmarshal(a, &packageAlias)
if err != nil {
return path, err
}

func UnaliasPath(path string, packageAlias map[string]string) (string, error) {
// An alias can come in different forms:
// - package
// - package/file.abs
Expand Down

0 comments on commit a28c2f4

Please sign in to comment.