MCPcopy
hub / github.com/larksuite/cli / parseFormSubmitJSON

Function parseFormSubmitJSON

shortcuts/base/base_form_submit.go:97–138  ·  view source on GitHub ↗

parseFormSubmitJSON 将 --json 解析为字段和附件映射。

(runtime *common.RuntimeContext)

Source from the content-addressed store, hash-verified

95
96// parseFormSubmitJSON 将 --json 解析为字段和附件映射。
97func 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
140func dryRunFormSubmit(_ context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
141 fields, attachmentMap, err := parseFormSubmitJSON(runtime)

Callers 3

dryRunFormSubmitFunction · 0.85
executeFormSubmitFunction · 0.85
TestParseFormSubmitJSONFunction · 0.85

Calls 4

newParseCtxFunction · 0.85
parseJSONObjectFunction · 0.85
baseFlagErrorfFunction · 0.85
StrMethod · 0.65

Tested by 1

TestParseFormSubmitJSONFunction · 0.68