MCPcopy Create free account
hub / github.com/cli/cli / StructExportData

Function StructExportData

pkg/cmdutil/json_flags.go:306–323  ·  view source on GitHub ↗

Basic function that can be used with structs that need to implement the exportable interface. It has numerous limitations so verify that it works as expected with the struct and fields you want to export. If it does not, then implementing a custom ExportData method is necessary. Perhaps this should

(s any, fields []string)

Source from the content-addressed store, hash-verified

304// a struct does not implement the exportable interface, but for now it will
305// need to be explicitly used.
306func StructExportData(s any, fields []string) map[string]any {
307 v := reflect.ValueOf(s)
308 if v.Kind() == reflect.Pointer {
309 v = v.Elem()
310 }
311 if v.Kind() != reflect.Struct {
312 // If s is not a struct or pointer to a struct return nil.
313 return nil
314 }
315 data := make(map[string]any, len(fields))
316 for _, f := range fields {
317 sf := fieldByName(v, f)
318 if sf.IsValid() && sf.CanInterface() {
319 data[f] = sf.Interface()
320 }
321 }
322 return data
323}
324
325func fieldByName(v reflect.Value, field string) reflect.Value {
326 return v.FieldByNameFunc(func(s string) bool {

Callers 11

ExportDataMethod · 0.92
ExportDataMethod · 0.92
ExportDataMethod · 0.92
ExportDataMethod · 0.92
ExportDataMethod · 0.92
ExportDataMethod · 0.92
ExportDataMethod · 0.92
ExportDataMethod · 0.92
ExportDataMethod · 0.92
ExportDataMethod · 0.92
TestStructExportDataFunction · 0.85

Calls 1

fieldByNameFunction · 0.70

Tested by 1

TestStructExportDataFunction · 0.68