gitee.com/eden-framework/sqlx@v0.0.3/builder/toggles.go (about) 1 package builder 2 3 import ( 4 "context" 5 ) 6 7 var ( 8 keyForToggles = "$$builder.toggles" 9 ) 10 11 var ( 12 ToggleMultiTable = "MultiTable" 13 ToggleNeedAutoAlias = "NeedAlias" 14 ToggleUseValues = "UseValues" 15 ) 16 17 type Toggles map[string]bool 18 19 func (toggles Toggles) Merge(next Toggles) Toggles { 20 final := Toggles{} 21 22 for k, v := range toggles { 23 if v { 24 final[k] = true 25 } 26 } 27 28 for k, v := range next { 29 if v { 30 final[k] = true 31 } else { 32 delete(final, k) 33 } 34 } 35 36 return final 37 } 38 39 func (toggles Toggles) Is(key string) bool { 40 if v, ok := toggles[key]; ok { 41 return v 42 } 43 return false 44 } 45 46 func ContextWithToggles(ctx context.Context, toggles Toggles) context.Context { 47 return context.WithValue(ctx, keyForToggles, TogglesFromContext(ctx).Merge(toggles)) 48 } 49 50 func TogglesFromContext(ctx context.Context) Toggles { 51 if ctx == nil { 52 return Toggles{} 53 } 54 if toggles, ok := ctx.Value(keyForToggles).(Toggles); ok { 55 return toggles 56 } 57 return Toggles{} 58 }