github.com/Rookout/GoSDK@v0.1.48/pkg/processor/operations/set.go (about) 1 package operations 2 3 import ( 4 "github.com/Rookout/GoSDK/pkg/config" 5 "github.com/Rookout/GoSDK/pkg/logger" 6 "github.com/Rookout/GoSDK/pkg/processor/namespaces" 7 "github.com/Rookout/GoSDK/pkg/processor/paths" 8 "github.com/Rookout/GoSDK/pkg/rookoutErrors" 9 "github.com/Rookout/GoSDK/pkg/types" 10 ) 11 12 type pathPair struct { 13 source paths.Path 14 dest paths.Path 15 } 16 17 type Operation interface { 18 Execute(namespace namespaces.Namespace) 19 } 20 21 type pathFactory interface { 22 GetPath(string) (paths.Path, rookoutErrors.RookoutError) 23 } 24 25 type Set struct { 26 pathList []pathPair 27 } 28 29 type ObjectNamespace interface { 30 GetObjectDumpConfig() config.ObjectDumpConfig 31 SetObjectDumpConfig(_ config.ObjectDumpConfig) 32 } 33 34 func NewSet(configuration types.AugConfiguration, factory pathFactory) (*Set, rookoutErrors.RookoutError) { 35 set := Set{} 36 37 paths, ok := configuration["paths"].(map[string]interface{}) 38 if !ok { 39 return nil, rookoutErrors.NewRookInvalidOperationConfiguration(configuration) 40 } 41 for dest, source := range paths { 42 destPath, err := factory.GetPath(dest) 43 if err != nil { 44 logger.Logger().WithError(err).Warningf("Failed to get path: %v\n", dest) 45 46 continue 47 } 48 sourceString, ok := source.(string) 49 if !ok { 50 return nil, rookoutErrors.NewRookInvalidOperationConfiguration(configuration) 51 } 52 sourcePath, err := factory.GetPath(sourceString) 53 if err != nil { 54 logger.Logger().WithError(err).Warningf("Failed to get path: %s\n", sourceString) 55 56 continue 57 } 58 set.pathList = append(set.pathList, pathPair{dest: destPath, source: sourcePath}) 59 } 60 61 return &set, nil 62 } 63 64 func (s *Set) Execute(namespace namespaces.Namespace) { 65 for _, pathPair := range s.pathList { 66 value, err := pathPair.source.ReadFrom(namespace) 67 if err != nil { 68 logger.Logger().WithError(err).Errorf("Failed to execute dest:source path pair: %v, %v\n", 69 pathPair.source, pathPair.dest) 70 71 continue 72 } 73 74 err = pathPair.dest.WriteTo(namespace, value) 75 if err != nil { 76 logger.Logger().WithError(err).Errorf("Failed to execute dest:source path pair: %v, %v\n", 77 pathPair.source, pathPair.dest) 78 79 continue 80 } 81 } 82 }