Documentation
¶
Index ¶
- Variables
- func BuildItemValue(dataInfo *DataInfo, itemData *FieldValueMap) (*reflect.Value, error)
- func TransformReaderEncoding(r io.Reader, encoding encoding.Encoding) (err error, newReader io.Reader)
- type DataInfo
- type DataLoader
- type FieldTag
- type FieldTagMap
- type FieldTagReadValueFunc
- type FieldValueMap
- type FlowControl
- type ItemFilter
- type ItemFilters
- type Unmarshaler
- type Var
- type VarMap
- type VarMaps
- type VarParseFunc
- type VarPattern
- type Vars
Constants ¶
This section is empty.
Variables ¶
var DefaultNonUtfEncoding = simplifiedchinese.GBK
var DefaultVarPattern = VarPattern{ Pattern: regexp.MustCompile(`(\w+)\[(\d+)?:(\d+)?(:\d+)?\]`), ParseFunc: func(v []string) *Var { if len(v) != 5 { return nil } var min = 0 var max = 999999 var step = 1 if v[2] != "" { min, _ = strconv.Atoi(v[2]) } if v[3] != "" { max, _ = strconv.Atoi(v[3]) } if v[4] != "" { step, _ = strconv.Atoi(v[4][1:]) } if step == 0 { step = 1 } if max < min { min, max = max, min } return &Var{ Name: v[1], Match: v[0], Min: min, Max: max, Step: step, } }, }
默认的变量匹配表达式,格式为:VarName[min:max:step],min和max可以为空,默认值为0,999999
Functions ¶
func BuildItemValue ¶
func BuildItemValue(dataInfo *DataInfo, itemData *FieldValueMap) (*reflect.Value, error)
根据 map[fieldIndex]fieldStringValue 组装对象元素
Types ¶
type DataInfo ¶
type DataInfo struct { Data interface{} Target reflect.Value DataType reflect.Type DataIsSlice bool ItemType reflect.Type ItemIsPtr bool ItemStructType reflect.Type BaseTags FieldTagMap }
func GetDataInfo ¶
data必须可写,包括指针、数组,必须是Struct 或者 Slice of Struct
type DataLoader ¶
type DataLoader struct { Data interface{} WriteData bool VarOrder []string ExitNoDataTimes int ReadValueFunc map[string]FieldTagReadValueFunc ItemFilters }
负责遍历所有变量,反序列化数据,调用ItemFilters处理每个元素,并最终根据writeData开关决定是否写入DataInfo.Target; 将writeData设为false,通过filter处理每个元素,可避免内存浪费,适合用来处理超大列表; WriteData,是否将数据写入;DataInfo.Target,关闭后可以减少内存占用; DataInfo,写入目标对象信息,包含对象的类型、指针、字段注释等; VarOrder,用于控制多个变量嵌套循环顺序,如果数组为空,按随机顺序嵌套循环; FieldTagReadValueFuncMap,结构为map[tag]FieldTagReadValueFunc,定义每种tag到读取方法,tag包括excel、xpath等; ItemFilters,处理每个的原始数据、转换对象,如果任一返回false,中断处理,不论data元素是不是地址类型 filter入参总是*interface{}指针,在校验原始数据时,入仓类型是*FieldValueMap,在校验元素对象时,类型是*YourModel,非已知类型最好返回(Forward,0);
func (*DataLoader) Load ¶
func (loader *DataLoader) Load() error
迭代DataLoader.DataInfo.BaseTags.MergeVars 根据DataLoader.DataInfo、Vars、文档读取方法,获取对象实例 调用DataLoader.filters,处理元素,并控制读取流程
type FieldTag ¶
type FieldTag struct { Id int //field index FieldName string //field name FieldType reflect.Type // Schema string // zip | xls | xlsx | xpath | csv Path string //position expression; e.g. sheet[x:x]/row[3:x]/col[3] PathFilled string //position expression filled vars; e.g. sheet[1]/row[2]/col[3] Pattern *regexp.Regexp //find value PatternIdx int //find value, default 0 PatternGroupIdx int //find value, default 0 Format string //format value, only for time Timezone *time.Location //default +8, only for time Vars *VarMap //var map in Path }
type FieldTagMap ¶
func GetFieldTags ¶
func GetFieldTags(T reflect.Type, tag string, varPattern *VarPattern) (*FieldTagMap, error)
获取所有字段的FieldTag map,允许定义多个path schema T,目标对象类型 tag, unmarshal tag is 'xm' varPattern, 定义path中的变量格式,表达式格式为:LTag(vName)[(vMin):(vMax)]RTag,例如 \{(\w+)\[(\d+):(\d+)(:(\d+))?\]\} 需确保匹配项长度为4,0可用于整体替换,1表示变量名,2表示变量下限,3表示变量上限
func (*FieldTagMap) Filter ¶
func (m *FieldTagMap) Filter(f func(tag *FieldTag) bool) *FieldTagMap
func (*FieldTagMap) MergeVars ¶
func (m *FieldTagMap) MergeVars() *VarMap
func (*FieldTagMap) SetValues ¶
func (m *FieldTagMap) SetValues(vs *Vars) *FieldTagMap
type FieldTagReadValueFunc ¶
用于读取一个字段的内容字符串,实现方法由具体场景业务提供
type FieldValueMap ¶
每个map表示一个item数据,key表示field index,value表示内容字符串, 由FieldUnmarshalTag负责解析value并生成元素对象,
type FlowControl ¶
type FlowControl int
用于控制列表处理过程
const ( // go on processing Forward FlowControl = 1 + iota // continue loop Continue // break loop Break )
func ExitIfFieldValueStringMapIsEmpty ¶
func ExitIfFieldValueStringMapIsEmpty(item interface{}, vars *Vars) (flow FlowControl, deep int)
func ExitIfItemIsNil ¶
func ExitIfItemIsNil(item interface{}, vars *Vars) (flow FlowControl, deep int)
type ItemFilter ¶
type ItemFilter func(item interface{}, vars *Vars) (flow FlowControl, deep int)
用于处理Item,可控制是否继续处理流程
type ItemFilters ¶
type ItemFilters []ItemFilter
func (ItemFilters) Filter ¶
func (filters ItemFilters) Filter(item interface{}, vars *Vars) (flow FlowControl, deep int)
type Unmarshaler ¶
type Var ¶
FieldName:变量名,出现重名,会被覆盖; Match:匹配到的表达式,用于整体替换; Min:变量下线; Max:变量上限; Val:自动生成的遍历值 Step:增长步长,默认为1
type VarParseFunc ¶
type VarPattern ¶
type VarPattern struct { Pattern *regexp.Regexp ParseFunc VarParseFunc }
Pattern:变量正则表达式,用于匹配变量定义 ParseFunc:将匹配的字符串数组,转化为Var对象
type Vars ¶
type Vars []*Var
set vars := [ v0, v1, v2, v2 ] then v0.deep=0, v1.deep=1, v2.deep=2, v2.deep=3
break (deep=0) -> { v0.Max(), v1.Max(), v2.Max(), v3.Max(), vars.Next() } continue (deep=0) -> { v1.Max(), v2.Max(), v3.Max(), vars.Next() }
break (deep=1) -> { v1.Max(), v2.Max(), v3.Max(), vars.Next() } continue (deep=1) -> { v2.Max(), v3.Max(), vars.Next() }
break (deep=2) -> { v2.Max(), v3.Max(), vars.Next() } continue (deep=2) -> { v3.Max(), vars.Next() }
break (deep=3) -> { v3.Max(), vars.Next() } continue (deep=3) -> { vars.Next() }