github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/label/shared.go (about)

     1  package label
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"time"
     7  )
     8  
     9  var labelFields = []string{
    10  	"color",
    11  	"createdAt",
    12  	"description",
    13  	"id",
    14  	"isDefault",
    15  	"name",
    16  	"updatedAt",
    17  	"url",
    18  }
    19  
    20  type label struct {
    21  	Color       string    `json:"color"`
    22  	CreatedAt   time.Time `json:"createdAt"`
    23  	Description string    `json:"description"`
    24  	ID          string    `json:"node_id"`
    25  	IsDefault   bool      `json:"isDefault"`
    26  	Name        string    `json:"name"`
    27  	URL         string    `json:"url"`
    28  	UpdatedAt   time.Time `json:"updatedAt"`
    29  }
    30  
    31  // ExportData implements cmdutil.exportable
    32  func (l *label) ExportData(fields []string) map[string]interface{} {
    33  	v := reflect.ValueOf(l).Elem()
    34  	data := map[string]interface{}{}
    35  
    36  	for _, f := range fields {
    37  		switch f {
    38  		default:
    39  			sf := fieldByName(v, f)
    40  			data[f] = sf.Interface()
    41  		}
    42  	}
    43  
    44  	return data
    45  }
    46  
    47  func fieldByName(v reflect.Value, field string) reflect.Value {
    48  	return v.FieldByNameFunc(func(s string) bool {
    49  		return strings.EqualFold(field, s)
    50  	})
    51  }