|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "google.golang.org/api/sheets/v4" |
| 9 | +) |
| 10 | + |
| 11 | +func applyForceSendFields(format *sheets.CellFormat, fieldMask string) error { |
| 12 | + if format == nil { |
| 13 | + return fmt.Errorf("format is required") |
| 14 | + } |
| 15 | + |
| 16 | + for _, raw := range splitFieldMask(fieldMask) { |
| 17 | + normalized := normalizeFormatField(raw) |
| 18 | + if normalized == "" { |
| 19 | + continue |
| 20 | + } |
| 21 | + if err := forceSendJSONField(format, normalized); err != nil { |
| 22 | + return fmt.Errorf("invalid format field %q: %w", strings.TrimSpace(raw), err) |
| 23 | + } |
| 24 | + } |
| 25 | + return nil |
| 26 | +} |
| 27 | + |
| 28 | +func splitFieldMask(mask string) []string { |
| 29 | + if strings.TrimSpace(mask) == "" { |
| 30 | + return nil |
| 31 | + } |
| 32 | + parts := strings.Split(mask, ",") |
| 33 | + for i := range parts { |
| 34 | + parts[i] = strings.TrimSpace(parts[i]) |
| 35 | + } |
| 36 | + return parts |
| 37 | +} |
| 38 | + |
| 39 | +func normalizeFormatField(field string) string { |
| 40 | + field = strings.TrimSpace(field) |
| 41 | + if field == "" { |
| 42 | + return "" |
| 43 | + } |
| 44 | + if field == "userEnteredFormat" { |
| 45 | + return "" |
| 46 | + } |
| 47 | + if strings.HasPrefix(field, "userEnteredFormat.") { |
| 48 | + return strings.TrimPrefix(field, "userEnteredFormat.") |
| 49 | + } |
| 50 | + return "" |
| 51 | +} |
| 52 | + |
| 53 | +func forceSendJSONField(root any, jsonPath string) error { |
| 54 | + current := reflect.ValueOf(root) |
| 55 | + if current.Kind() != reflect.Pointer || current.IsNil() { |
| 56 | + return fmt.Errorf("format must be a non-nil pointer") |
| 57 | + } |
| 58 | + |
| 59 | + parts := strings.Split(jsonPath, ".") |
| 60 | + for i, part := range parts { |
| 61 | + if current.Kind() == reflect.Pointer { |
| 62 | + if current.IsNil() { |
| 63 | + if current.Type().Elem().Kind() != reflect.Struct { |
| 64 | + return fmt.Errorf("field %q is not a struct", part) |
| 65 | + } |
| 66 | + current.Set(reflect.New(current.Type().Elem())) |
| 67 | + } |
| 68 | + current = current.Elem() |
| 69 | + } |
| 70 | + if current.Kind() != reflect.Struct { |
| 71 | + return fmt.Errorf("field %q is not a struct", part) |
| 72 | + } |
| 73 | + |
| 74 | + fieldValue, fieldName, ok := findJSONField(current, part) |
| 75 | + if !ok { |
| 76 | + return fmt.Errorf("unknown field %q", part) |
| 77 | + } |
| 78 | + |
| 79 | + if i == len(parts)-1 { |
| 80 | + if fieldValue.Kind() == reflect.Pointer && fieldValue.IsNil() && fieldValue.Type().Elem().Kind() == reflect.Struct { |
| 81 | + fieldValue.Set(reflect.New(fieldValue.Type().Elem())) |
| 82 | + } |
| 83 | + return addForceSendField(current, fieldName) |
| 84 | + } |
| 85 | + |
| 86 | + switch fieldValue.Kind() { |
| 87 | + case reflect.Pointer: |
| 88 | + if fieldValue.IsNil() { |
| 89 | + if fieldValue.Type().Elem().Kind() != reflect.Struct { |
| 90 | + return fmt.Errorf("field %q is not a struct", part) |
| 91 | + } |
| 92 | + fieldValue.Set(reflect.New(fieldValue.Type().Elem())) |
| 93 | + } |
| 94 | + current = fieldValue |
| 95 | + case reflect.Struct: |
| 96 | + if !fieldValue.CanAddr() { |
| 97 | + return fmt.Errorf("field %q is not addressable", part) |
| 98 | + } |
| 99 | + current = fieldValue.Addr() |
| 100 | + default: |
| 101 | + return fmt.Errorf("field %q is not a struct", part) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func findJSONField(v reflect.Value, jsonName string) (reflect.Value, string, bool) { |
| 109 | + t := v.Type() |
| 110 | + for i := 0; i < t.NumField(); i++ { |
| 111 | + field := t.Field(i) |
| 112 | + if field.PkgPath != "" { |
| 113 | + continue |
| 114 | + } |
| 115 | + tag := field.Tag.Get("json") |
| 116 | + if tag == "-" { |
| 117 | + continue |
| 118 | + } |
| 119 | + name := strings.Split(tag, ",")[0] |
| 120 | + if name == "" { |
| 121 | + continue |
| 122 | + } |
| 123 | + if name == jsonName { |
| 124 | + return v.Field(i), field.Name, true |
| 125 | + } |
| 126 | + } |
| 127 | + return reflect.Value{}, "", false |
| 128 | +} |
| 129 | + |
| 130 | +func addForceSendField(v reflect.Value, fieldName string) error { |
| 131 | + fs := v.FieldByName("ForceSendFields") |
| 132 | + if !fs.IsValid() { |
| 133 | + return fmt.Errorf("missing ForceSendFields") |
| 134 | + } |
| 135 | + if fs.Kind() != reflect.Slice || fs.Type().Elem().Kind() != reflect.String { |
| 136 | + return fmt.Errorf("invalid ForceSendFields") |
| 137 | + } |
| 138 | + for i := 0; i < fs.Len(); i++ { |
| 139 | + if fs.Index(i).String() == fieldName { |
| 140 | + return nil |
| 141 | + } |
| 142 | + } |
| 143 | + fs.Set(reflect.Append(fs, reflect.ValueOf(fieldName))) |
| 144 | + return nil |
| 145 | +} |
0 commit comments