github.com/blend/go-sdk@v1.20240719.1/web/view_cache_option.go (about) 1 /* 2 3 Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package web 9 10 import "html/template" 11 12 // ViewCacheOption is an option for ViewCache. 13 type ViewCacheOption func(*ViewCache) error 14 15 // OptViewCachePaths sets the view cache paths. 16 func OptViewCachePaths(paths ...string) ViewCacheOption { 17 return func(vc *ViewCache) error { vc.Paths = append(vc.Paths, paths...); return nil } 18 } 19 20 // OptViewCacheLiterals sets the view cache literals. 21 func OptViewCacheLiterals(literals ...string) ViewCacheOption { 22 return func(vc *ViewCache) error { vc.Literals = append(vc.Literals, literals...); return nil } 23 } 24 25 // OptViewCacheFuncMap sets the view cache func maps. 26 func OptViewCacheFuncMap(funcMap template.FuncMap) ViewCacheOption { 27 return func(vc *ViewCache) error { vc.FuncMap = funcMap; return nil } 28 } 29 30 // OptViewCacheFunc adds a func to the view func map. 31 func OptViewCacheFunc(name string, viewFunc interface{}) ViewCacheOption { 32 return func(vc *ViewCache) error { 33 if vc.FuncMap == nil { 34 vc.FuncMap = make(template.FuncMap) 35 } 36 vc.FuncMap[name] = viewFunc 37 return nil 38 } 39 } 40 41 // OptViewCacheLiveReload adds a func to the view func map. 42 func OptViewCacheLiveReload(liveReload bool) ViewCacheOption { 43 return func(vc *ViewCache) error { vc.LiveReload = liveReload; return nil } 44 } 45 46 // OptViewCacheInternalErrorTemplateName sets the internal error template name. 47 func OptViewCacheInternalErrorTemplateName(name string) ViewCacheOption { 48 return func(vc *ViewCache) error { vc.InternalErrorTemplateName = name; return nil } 49 } 50 51 // OptViewCacheBadRequestTemplateName sets the bad request template name. 52 func OptViewCacheBadRequestTemplateName(name string) ViewCacheOption { 53 return func(vc *ViewCache) error { vc.BadRequestTemplateName = name; return nil } 54 } 55 56 // OptViewCacheNotFoundTemplateName sets the not found template name. 57 func OptViewCacheNotFoundTemplateName(name string) ViewCacheOption { 58 return func(vc *ViewCache) error { vc.NotFoundTemplateName = name; return nil } 59 } 60 61 // OptViewCacheNotAuthorizedTemplateName sets the not authorized template name. 62 func OptViewCacheNotAuthorizedTemplateName(name string) ViewCacheOption { 63 return func(vc *ViewCache) error { vc.NotAuthorizedTemplateName = name; return nil } 64 } 65 66 // OptViewCacheStatusTemplateName sets the status template name. 67 func OptViewCacheStatusTemplateName(name string) ViewCacheOption { 68 return func(vc *ViewCache) error { vc.StatusTemplateName = name; return nil } 69 } 70 71 // OptViewCacheConfig sets options based on a config. 72 func OptViewCacheConfig(cfg *ViewCacheConfig) ViewCacheOption { 73 return func(vc *ViewCache) error { 74 vc.Paths = cfg.Paths 75 vc.LiveReload = cfg.LiveReload 76 vc.InternalErrorTemplateName = cfg.InternalErrorTemplateNameOrDefault() 77 vc.BadRequestTemplateName = cfg.BadRequestTemplateNameOrDefault() 78 vc.NotFoundTemplateName = cfg.NotFoundTemplateNameOrDefault() 79 vc.NotAuthorizedTemplateName = cfg.NotAuthorizedTemplateNameOrDefault() 80 vc.StatusTemplateName = cfg.StatusTemplateNameOrDefault() 81 return nil 82 } 83 }