diff --git a/cache.go b/cache.go index 55af84a..83ca900 100644 --- a/cache.go +++ b/cache.go @@ -32,16 +32,6 @@ func (item Item) Expired() bool { return item.Expiration != 0 && time.Now().UnixNano() > item.Expiration } -const ( - // NoExpiration is for use with functions that take no expiration time. - NoExpiration time.Duration = -1 - // DefaultExpiration is for use with functions that take an - // expiration time. Equivalent to passing in the same expiration - // duration as was given to New() when the cache was - // created (e.g. 5 minutes.) - DefaultExpiration time.Duration = 0 -) - // Cache struct type Cache_tpl struct { *cache diff --git a/cachemap/cache.tmpl b/cachemap/cache.tmpl index 1e10eaa..a7ec550 100644 --- a/cachemap/cache.tmpl +++ b/cachemap/cache.tmpl @@ -32,16 +32,6 @@ func (item Item) Expired() bool { return item.Expiration != 0 && time.Now().UnixNano() > item.Expiration } -const ( - // NoExpiration is for use with functions that take no expiration time. - NoExpiration time.Duration = -1 - // DefaultExpiration is for use with functions that take an - // expiration time. Equivalent to passing in the same expiration - // duration as was given to New() when the cache was - // created (e.g. 5 minutes.) - DefaultExpiration time.Duration = 0 -) - // Cache struct type {{.ValueType}}Cache struct { *cache diff --git a/cachemap/const.tmpl b/cachemap/const.tmpl new file mode 100644 index 0000000..8b61a4a --- /dev/null +++ b/cachemap/const.tmpl @@ -0,0 +1,19 @@ +package {{.PackageName}} + +import ( + "time" +) + +// To avoid redecleration, put common code in this file. + +const ( + // NoExpiration is for use with functions that take no expiration time. + NoExpiration time.Duration = -1 + // DefaultExpiration is for use with functions that take an + // expiration time. Equivalent to passing in the same expiration + // duration as was given to New() when the cache was + // created (e.g. 5 minutes.) + DefaultExpiration time.Duration = 0 +) + + diff --git a/cachemap/main.go b/cachemap/main.go index deab2ba..18b4ddf 100644 --- a/cachemap/main.go +++ b/cachemap/main.go @@ -157,7 +157,7 @@ FIND: if err != nil { fatal(err) } - if isBuiltin(*valueType) { + if !isBuiltin(*valueType) { *valueType = strings.Title(*valueType) } err = tpl.Execute( @@ -174,4 +174,20 @@ FIND: if err != nil { fatal(err) } + constFile, err := os.OpenFile(fmt.Sprintf("cachemap_const.go"), os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644) + if err != nil { + fatal(err) + } + defer constFile.Close() + constTpl, err := template.New("const.tmpl").ParseFiles(filepath.Join(packageDir(), "const.tmpl")) + if err != nil { + fatal(err) + } + err = constTpl.Execute(constFile, + map[string]interface{}{ + "PackageName": packageName, + }) + if err != nil { + fatal(err) + } } diff --git a/const.go b/const.go new file mode 100644 index 0000000..22b8518 --- /dev/null +++ b/const.go @@ -0,0 +1,17 @@ +package cache + +import ( + "time" +) + +// To avoid redecleration errors, put common code in this file. + +const ( + // NoExpiration is for use with functions that take no expiration time. + NoExpiration time.Duration = -1 + // DefaultExpiration is for use with functions that take an + // expiration time. Equivalent to passing in the same expiration + // duration as was given to New() when the cache was + // created (e.g. 5 minutes.) + DefaultExpiration time.Duration = 0 +)