chore: 并入 iotlcm-admin-svr 历史到 admin-svr/(原提交 49818c6)
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ValidateRequiredFields(rows []ParsedRow, specs []ColumnSpec) []RowIssue {
|
||||
issues := make([]RowIssue, 0)
|
||||
for _, row := range rows {
|
||||
for _, spec := range specs {
|
||||
if !spec.Required {
|
||||
continue
|
||||
}
|
||||
value := Field(row, spec.Name)
|
||||
if value != "" {
|
||||
continue
|
||||
}
|
||||
issues = append(issues, RowIssue{
|
||||
RowNo: row.RowNo,
|
||||
Column: spec.Name,
|
||||
Value: value,
|
||||
Message: fmt.Sprintf("%s is required", spec.Name),
|
||||
})
|
||||
}
|
||||
}
|
||||
return issues
|
||||
}
|
||||
|
||||
func ValidateRowTypes(rows []ParsedRow, specs []ColumnSpec) []RowIssue {
|
||||
issues := make([]RowIssue, 0)
|
||||
for _, row := range rows {
|
||||
for _, spec := range specs {
|
||||
value := Field(row, spec.Name)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if _, err := normalizedValue(value, spec.Type); err != nil {
|
||||
issues = append(issues, RowIssue{
|
||||
RowNo: row.RowNo,
|
||||
Column: spec.Name,
|
||||
Value: value,
|
||||
Message: fmt.Sprintf("%s format is invalid", spec.Name),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return issues
|
||||
}
|
||||
|
||||
func ValidateFileConsistency(rows []ParsedRow, rules []ConsistencyRule) []RowIssue {
|
||||
issues := make([]RowIssue, 0)
|
||||
for _, rule := range rules {
|
||||
baseValue := ""
|
||||
baseSet := false
|
||||
for _, row := range rows {
|
||||
raw := Field(row, rule.Name)
|
||||
normalized := ""
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
if !rule.AllowEmpty {
|
||||
issues = append(issues, RowIssue{
|
||||
RowNo: row.RowNo,
|
||||
Column: rule.Name,
|
||||
Value: raw,
|
||||
Message: fmt.Sprintf("%s must not be empty", rule.Name),
|
||||
})
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
value, err := normalizedValue(raw, rule.Type)
|
||||
if err != nil {
|
||||
issues = append(issues, RowIssue{
|
||||
RowNo: row.RowNo,
|
||||
Column: rule.Name,
|
||||
Value: raw,
|
||||
Message: fmt.Sprintf("%s format is invalid", rule.Name),
|
||||
})
|
||||
continue
|
||||
}
|
||||
normalized = value
|
||||
}
|
||||
|
||||
if !baseSet {
|
||||
baseValue = normalized
|
||||
baseSet = true
|
||||
continue
|
||||
}
|
||||
if normalized != baseValue {
|
||||
issues = append(issues, RowIssue{
|
||||
RowNo: row.RowNo,
|
||||
Column: rule.Name,
|
||||
Value: raw,
|
||||
Message: fmt.Sprintf("%s must be consistent", rule.Name),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return issues
|
||||
}
|
||||
Reference in New Issue
Block a user