parseFormSubmitJSON 将 --json 解析为字段和附件映射。
(runtime *common.RuntimeContext)
| 95 | |
| 96 | // parseFormSubmitJSON 将 --json 解析为字段和附件映射。 |
| 97 | func parseFormSubmitJSON(runtime *common.RuntimeContext) (map[string]interface{}, map[string][]string, error) { |
| 98 | pc := newParseCtx(runtime) |
| 99 | raw, err := parseJSONObject(pc, runtime.Str("json"), "json") |
| 100 | if err != nil { |
| 101 | return nil, nil, err |
| 102 | } |
| 103 | |
| 104 | fields, _ := raw["fields"].(map[string]interface{}) |
| 105 | if fields == nil { |
| 106 | fields = make(map[string]interface{}) |
| 107 | } |
| 108 | |
| 109 | var attMap map[string][]string |
| 110 | if attachments, ok := raw["attachments"]; ok { |
| 111 | attObj, ok := attachments.(map[string]interface{}) |
| 112 | if !ok { |
| 113 | return nil, nil, baseFlagErrorf(`--json.attachments must be a JSON object mapping field names to file path arrays`) |
| 114 | } |
| 115 | if len(attObj) > 0 { |
| 116 | attMap = make(map[string][]string, len(attObj)) |
| 117 | for fieldName, value := range attObj { |
| 118 | paths, ok := value.([]interface{}) |
| 119 | if !ok { |
| 120 | return nil, nil, baseFlagErrorf("--json.attachments.%q must be a file path array, got %T", fieldName, value) |
| 121 | } |
| 122 | filePaths := make([]string, 0, len(paths)) |
| 123 | for _, item := range paths { |
| 124 | if s, ok := item.(string); ok { |
| 125 | filePaths = append(filePaths, s) |
| 126 | } else { |
| 127 | return nil, nil, baseFlagErrorf("--json.attachments.%q must contain file path strings only, got %T", fieldName, item) |
| 128 | } |
| 129 | } |
| 130 | if len(filePaths) > 0 { |
| 131 | attMap[fieldName] = filePaths |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return fields, attMap, nil |
| 138 | } |
| 139 | |
| 140 | func dryRunFormSubmit(_ context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { |
| 141 | fields, attachmentMap, err := parseFormSubmitJSON(runtime) |