github.com/Rookout/GoSDK@v0.1.48/pkg/augs/locations/location_factory.go (about) 1 package locations 2 3 import ( 4 "github.com/Rookout/GoSDK/pkg/augs" 5 "github.com/Rookout/GoSDK/pkg/augs/actions" 6 "github.com/Rookout/GoSDK/pkg/augs/conditions" 7 "github.com/Rookout/GoSDK/pkg/com_ws" 8 "github.com/Rookout/GoSDK/pkg/rookoutErrors" 9 "github.com/Rookout/GoSDK/pkg/types" 10 ) 11 12 type LocationFactory struct { 13 output com_ws.Output 14 processorFactory actions.ProcessorFactory 15 ConditionCreator conditions.ConditionCreatorFunc 16 AugCreator AugCreatorFunc 17 LocationFileLineCreator LocationFileLineCreatorFunc 18 ActionRunProcessorCreator ActionRunProcessorCreatorFunc 19 } 20 type ActionRunProcessorCreatorFunc func(configuration types.AugConfiguration, factory actions.ProcessorFactory) (actions.Action, rookoutErrors.RookoutError) 21 type AugCreatorFunc func(types.AugID, actions.Action, com_ws.Output) augs.Aug 22 type LocationFileLineCreatorFunc func(types.AugConfiguration, com_ws.Output, augs.Aug) (Location, rookoutErrors.RookoutError) 23 24 func NewLocationFactory(output com_ws.Output, processorFactory actions.ProcessorFactory) *LocationFactory { 25 return &LocationFactory{ 26 output: output, 27 processorFactory: processorFactory, 28 ConditionCreator: conditions.NewCondition, 29 AugCreator: augs.NewAug, 30 LocationFileLineCreator: NewLocationFileLine, 31 ActionRunProcessorCreator: actions.NewActionRunProcessor, 32 } 33 } 34 35 func (l *LocationFactory) GetLocation(configuration types.AugConfiguration) (Location, rookoutErrors.RookoutError) { 36 var err error 37 38 augID, ok := configuration["id"].(string) 39 if !ok { 40 return nil, rookoutErrors.NewRookAugInvalidKey("id", configuration) 41 } 42 43 actionConfig, ok := configuration["action"].(map[string]interface{}) 44 if !ok { 45 return nil, rookoutErrors.NewRookAugInvalidKey("action", configuration) 46 } 47 48 action, err := l.ActionRunProcessorCreator(actionConfig, l.processorFactory) 49 if err != nil { 50 return nil, err.(rookoutErrors.RookoutError) 51 } 52 53 aug := l.AugCreator(augID, action, l.output) 54 55 limitsManager, err := augs.GetLimitProvider().GetLimitManager(configuration, augID, l.output) 56 if err != nil { 57 return nil, err.(rookoutErrors.RookoutError) 58 } 59 60 aug.SetLimitsManager(limitsManager) 61 62 conditionConfig, ok := configuration["conditional"].(string) 63 if ok { 64 condition, err := l.ConditionCreator(conditionConfig) 65 if err != nil { 66 return nil, err 67 } 68 aug.SetCondition(condition) 69 } 70 71 locationConfig, ok := configuration["location"].(map[string]interface{}) 72 if !ok { 73 return nil, rookoutErrors.NewRookAugInvalidKey("location", configuration) 74 } 75 76 return l.getLocation(locationConfig, aug) 77 } 78 79 func (l *LocationFactory) getLocation(configuration types.AugConfiguration, aug augs.Aug) (Location, rookoutErrors.RookoutError) { 80 name, ok := configuration["name"].(string) 81 if !ok { 82 return nil, rookoutErrors.NewRookObjectNameMissing(configuration) 83 } 84 85 switch name { 86 case "file_line": 87 return l.LocationFileLineCreator(configuration, l.output, aug) 88 89 default: 90 return nil, rookoutErrors.NewRookUnsupportedLocation(name) 91 } 92 }