-
Notifications
You must be signed in to change notification settings - Fork 4
/
decl.go
154 lines (132 loc) · 4.61 KB
/
decl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Package impex is a implementation of import/export service
package impex
import (
"github.com/ottemo/commerce/app/models"
"github.com/ottemo/commerce/env"
"io"
"regexp"
)
// Package global constants
const (
ConstErrorModule = "impex"
ConstErrorLevel = env.ConstErrorLevelService
ConstLogFileName = "impex.log"
constImportStateIdle = "idle"
constImportStateProcessing = "processing"
)
// Package global variables
var (
ConstImpexLog = true // flag indicates to make log of values going to be processed
ConstDebugLog = false // flag indicates to have extra log information
// ConstCSVColumnRegexp is a regular expression used to grab csv column information
//
// column format: [flags]path [memorize] [type] [convertors]
// [~|^|?][@a.b.c.]path [={name}|>{name}] [<{type}>]
//
// flags - optional column modificator
// format: [~|^|?]
// "~" - ignore column on collapse lookup
// "^" - array column
// "?" - maybe array column
//
// path - attribute name in result map sub-levels separated by "."
// format: [@a.b.c.]d
// "@a" - memorized value
//
// memorize - marks column to hold value in memorize map, these values can be used in path like "item.@value.label"
// format: ={name} | >{name}
// {name} - alphanumeric value
// ={name} - saves {column path} + {column value} to memorize map
// >{name} - saves {column value} to memorize map
//
// type - optional type for column
// format: <{type}>
// {type} - int | float | bool
//
// convertors - text template modifications you can apply to value before use it
// format: see (http://golang.org/pkg/text/template/)
ConstCSVColumnRegexp = regexp.MustCompile(`^\s*([~^?])?((?:@?\w+\.)*@?\w+)(\s+(?:=|>)\s*\w+)?(?:\s+<([^>]+)>)?\s*(.*)$`)
ConversionFuncs = map[string]interface{}{}
// set of service import commands
importCmd = make(map[string]InterfaceImpexImportCmd)
// set of models implementing InterfaceImpexModel
impexModels = make(map[string]InterfaceImpexModel)
// currently importing process status
importStatus StructImportStatus
)
// ImportCmdAttributeAdd is a implementer of InterfaceImpexImportCmd
// - command allows to create custom attributes on model
type ImportCmdAttributeAdd struct {
model models.InterfaceModel
attribute models.StructAttributeInfo
upsert bool
}
// ImportCmdImport is a implementer of InterfaceImpexImportCmd
// - command allows to work with InterfaceImpexModel instances
type ImportCmdImport struct {
model InterfaceImpexModel
attributes map[string]bool
}
// ImportCmdInsert is a implementer of InterfaceImpexImportCmd
// - command allows to upload data in system through model item abstraction
type ImportCmdInsert struct {
model models.InterfaceModel
attributes map[string]bool
skipErrors bool
}
// ImportCmdUpdate is a implementer of InterfaceImpexImportCmd
// - command allows to update model item data in system
type ImportCmdUpdate struct {
model models.InterfaceModel
attributes map[string]bool
idKey string
}
// ImportCmdUpsert is a implementer of InterfaceImpexImportCmd
// - command allows to update model item data in system by uniqueKeys or will insert new record
// - skipInsertErrors will continue processing in case of errors during insert
type ImportCmdUpsert struct {
model models.InterfaceModel
attributes map[string]bool
uniqueKeys []string
skipInsertErrors bool
}
// ImportCmdDelete is a implementer of InterfaceImpexImportCmd
// - command allows to delete model items from the system
type ImportCmdDelete struct {
model models.InterfaceModel
idKey string
}
// ImportCmdMedia is a implementer of InterfaceImpexImportCmd
// - command allows to assign media content to models item
type ImportCmdMedia struct {
mediaField string
mediaType string
mediaName string
skipErrors bool
}
// ImportCmdStore is a implementer of InterfaceImpexImportCmd
// - command allows temporary load/store previous command results during import process
type ImportCmdStore struct {
storeObjectAs string
storeValueAs map[string]string
prefix string
prefixKey string
}
// ImportCmdAlias is a implementer of InterfaceImpexImportCmd
// - command allows to make record field alias to object attribute value
type ImportCmdAlias struct {
aliases map[string]string
}
// StructImportingFile holds currently importing file information
type StructImportingFile struct {
reader io.Reader
size int64
name string
}
// StructImportStatus holds current import status.
// The result of import will be stored in "sessions" map per Session.
type StructImportStatus struct {
file *StructImportingFile
state string
sessions map[string]interface{}
}