| 
 | 1 | +package texttree  | 
 | 2 | + | 
 | 3 | +// AUTHOR: Daniil Furmanov  | 
 | 4 | +// LICENSE: MIT  | 
 | 5 | +// DESCRIPTION: TextTree is a file buffer that stores files content  | 
 | 6 | +// 				in memory and allow access to it by path.  | 
 | 7 | +//				It is useful for working with localization trees.  | 
 | 8 | + | 
 | 9 | +import (  | 
 | 10 | +	"io/ioutil"  | 
 | 11 | +	"os"  | 
 | 12 | +	"path/filepath"  | 
 | 13 | +)  | 
 | 14 | + | 
 | 15 | +const DefaultMaxFileSize = 16 << 10 // 16 KB  | 
 | 16 | + | 
 | 17 | +type TextTree struct {  | 
 | 18 | +	cache map[string]*Entity  | 
 | 19 | +	basePath string  | 
 | 20 | +}  | 
 | 21 | + | 
 | 22 | +type Entity struct {  | 
 | 23 | +	Content string  | 
 | 24 | +	Filename string  | 
 | 25 | +}  | 
 | 26 | + | 
 | 27 | +// Creates a new text tree by loading all the files from a specified directory  | 
 | 28 | +// that are smaller than maximum file size  | 
 | 29 | +func NewTextTree(path string, maxFileSize int64) (*TextTree, error) {  | 
 | 30 | +	cache := make(map[string]*Entity)  | 
 | 31 | +	// recursively walk through the path  | 
 | 32 | +	err := filepath.Walk(path,  | 
 | 33 | +		func(path string, info os.FileInfo, err error) error {  | 
 | 34 | +			if err != nil {  | 
 | 35 | +				return err  | 
 | 36 | +			}  | 
 | 37 | +			// check the file size  | 
 | 38 | +			if !info.IsDir() && info.Size() < maxFileSize {  | 
 | 39 | +				data, err := ioutil.ReadFile(path)  | 
 | 40 | +				if err != nil {  | 
 | 41 | +					return err  | 
 | 42 | +				}  | 
 | 43 | +				newEntity := &Entity{Content: string(data), Filename: info.Name()}  | 
 | 44 | +				dir, filename := filepath.Split(path)  | 
 | 45 | +				key := dir + filename[0:len(filename)-len(filepath.Ext(filename))]  | 
 | 46 | +				if entity, ok := cache[key]; ok {  | 
 | 47 | +					// if entity already exists, keep the key extension  | 
 | 48 | +					delete(cache, key)  | 
 | 49 | +					cache[path] = newEntity  | 
 | 50 | +					cache[dir+entity.Filename] = entity  | 
 | 51 | +				} else {  | 
 | 52 | +					cache[key] = newEntity  | 
 | 53 | +				}  | 
 | 54 | +			}  | 
 | 55 | +			return nil  | 
 | 56 | +		})  | 
 | 57 | +	if err != nil {  | 
 | 58 | +		return nil, err  | 
 | 59 | +	}  | 
 | 60 | +	// create a text tree object with buffered data  | 
 | 61 | +	return &TextTree{  | 
 | 62 | +		cache: cache,  | 
 | 63 | +		basePath: path,  | 
 | 64 | +	}, nil  | 
 | 65 | +}  | 
 | 66 | + | 
 | 67 | +// Creates an array of file paths  | 
 | 68 | +func (tt *TextTree) Entities() (entities []string) {  | 
 | 69 | +	entities = make([]string, len(tt.cache))  | 
 | 70 | +	i := 0  | 
 | 71 | +	for key := range tt.cache {  | 
 | 72 | +		entities[i] = key  | 
 | 73 | +		i++  | 
 | 74 | +	}  | 
 | 75 | +	return  | 
 | 76 | +}  | 
 | 77 | + | 
 | 78 | +// Returns an entity  | 
 | 79 | +func (tt *TextTree) Get(path string) *Entity {  | 
 | 80 | +	return tt.cache[path]  | 
 | 81 | +}  | 
 | 82 | + | 
 | 83 | +// Returns an entity if it exists  | 
 | 84 | +func (tt *TextTree) GetIfExists(path string) (*Entity, bool) {  | 
 | 85 | +	e, ok := tt.cache[path]  | 
 | 86 | +	return e, ok  | 
 | 87 | +}  | 
 | 88 | + | 
 | 89 | +// Returns an entity's content  | 
 | 90 | +func (tt *TextTree) GetString(path string) string {  | 
 | 91 | +	return tt.cache[path].Content  | 
 | 92 | +}  | 
 | 93 | + | 
 | 94 | +// Returns an entity's content if it exists  | 
 | 95 | +func (tt *TextTree) GetStringIfExists(path string) (string, bool) {  | 
 | 96 | +	e, ok := tt.cache[path]  | 
 | 97 | +	return e.Content, ok  | 
 | 98 | +}  | 
 | 99 | + | 
 | 100 | +// Returns the path of a loaded directory  | 
 | 101 | +func (tt *TextTree) GetBasePath() string {  | 
 | 102 | +	return tt.basePath  | 
 | 103 | +}  | 
0 commit comments