github.com/go-generalize/volcago@v1.7.0/generator/testfiles/not_auto/misc_gen.go (about)

     1  // Code generated by volcago. DO NOT EDIT.
     2  // generated version: devel
     3  package model
     4  
     5  import (
     6  	"reflect"
     7  	"strings"
     8  	"time"
     9  
    10  	"cloud.google.com/go/firestore"
    11  	"google.golang.org/genproto/googleapis/type/latlng"
    12  )
    13  
    14  // SetLastThreeToZero - set the last three digits to zero
    15  func SetLastThreeToZero(t time.Time) time.Time {
    16  	return time.Unix(t.Unix(), int64(t.Nanosecond()/1000*1000))
    17  }
    18  
    19  // GetOption - option to include logical deletion data
    20  type GetOption struct {
    21  	IncludeSoftDeleted bool
    22  }
    23  
    24  // DeleteMode - delete mode
    25  type DeleteMode int
    26  
    27  const (
    28  	DeleteModeSoft DeleteMode = iota + 1 // logical delete mode
    29  	DeleteModeHard                       // physical delete mode
    30  )
    31  
    32  // DeleteOption - option to delete logically or physically
    33  // default: physical deletion
    34  // use `DeleteModeSoft` or `DeleteModeHard
    35  type DeleteOption struct {
    36  	Mode DeleteMode
    37  }
    38  
    39  // PagingResult - paging result
    40  type PagingResult struct {
    41  	NextCursorKey string
    42  	Length        int
    43  }
    44  
    45  func isReservedType(value reflect.Value) bool {
    46  	switch value.Interface().(type) {
    47  	case time.Time, *time.Time,
    48  		latlng.LatLng, *latlng.LatLng,
    49  		firestore.DocumentRef, *firestore.DocumentRef:
    50  		return true
    51  	}
    52  	return false
    53  }
    54  
    55  func tagMap(v interface{}) map[string]string {
    56  	rv := reflect.Indirect(reflect.ValueOf(v))
    57  	rt := rv.Type()
    58  	tags := make(map[string]string)
    59  	for i := 0; i < rt.NumField(); i++ {
    60  		ft := rt.Field(i)
    61  		fv := rv.Field(i)
    62  		if ft.Anonymous {
    63  			for key, val := range tagMap(fv.Interface()) {
    64  				if _, ok := tags[key]; ok {
    65  					panic("fields with the same name cannot be used")
    66  				}
    67  				tags[key] = val
    68  			}
    69  			continue
    70  		}
    71  		tag := ft.Name
    72  		if firestoreTag, ok := ft.Tag.Lookup("firestore"); ok {
    73  			tag = strings.Split(firestoreTag, ",")[0]
    74  		}
    75  		switch fv.Kind() {
    76  		case reflect.Ptr:
    77  			ptrType := reflect.PtrTo(fv.Type()).Elem()
    78  			fv = reflect.New(ptrType.Elem())
    79  			fallthrough
    80  		case reflect.Struct:
    81  			if isReservedType(fv) {
    82  				break
    83  			}
    84  			for key, value := range tagMap(fv.Interface()) {
    85  				compositeKey := strings.Join([]string{tag, key}, ".")
    86  				if _, ok := tags[compositeKey]; ok {
    87  					panic("fields with the same name cannot be used")
    88  				}
    89  				compositeValue := strings.Join([]string{tag, value}, ".")
    90  				tags[compositeKey] = compositeValue
    91  			}
    92  			continue
    93  		}
    94  		if _, ok := tags[ft.Name]; ok {
    95  			panic("fields with the same name cannot be used")
    96  		}
    97  		tags[ft.Name] = tag
    98  	}
    99  	return tags
   100  }
   101  
   102  func updater(v, param interface{}) []firestore.Update {
   103  	updates := make([]firestore.Update, 0)
   104  	for _, update := range updateBuilder(v, param) {
   105  		updates = append(updates, update)
   106  	}
   107  	return updates
   108  }
   109  
   110  func updateBuilder(v, param interface{}) map[string]firestore.Update {
   111  	rv := reflect.Indirect(reflect.ValueOf(v))
   112  	rt := rv.Type()
   113  	pv := reflect.Indirect(reflect.ValueOf(param))
   114  	pt := pv.Type()
   115  	updateMap := make(map[string]firestore.Update)
   116  	for i := 0; i < rt.NumField(); i++ {
   117  		ft := rt.Field(i)
   118  		fv := rv.Field(i)
   119  
   120  		if ft.Anonymous {
   121  			for key, val := range updateBuilder(fv.Interface(), param) {
   122  				if _, ok := updateMap[key]; ok {
   123  					panic("fields with the same name cannot be used")
   124  				}
   125  				updateMap[key] = val
   126  			}
   127  			continue
   128  		}
   129  
   130  		if _, ok := pt.FieldByName(ft.Name); !ok {
   131  			continue
   132  		}
   133  
   134  		path := ft.Name
   135  		if firestoreTag, ok := ft.Tag.Lookup("firestore"); ok {
   136  			path = strings.Split(firestoreTag, ",")[0]
   137  		}
   138  
   139  		pfv := pv.FieldByName(ft.Name)
   140  		if pfv.Interface() == nil && !isReservedType(fv) && fv.Kind() == reflect.Ptr {
   141  			pfv.Set(reflect.New(fv.Type().Elem()))
   142  		}
   143  
   144  		switch fv.Kind() {
   145  		case reflect.Ptr:
   146  			ptrType := reflect.PtrTo(fv.Type()).Elem()
   147  			fv = reflect.New(ptrType.Elem())
   148  			fallthrough
   149  		case reflect.Struct:
   150  			if isReservedType(fv) {
   151  				break
   152  			}
   153  			if pfv.Interface() == nil {
   154  				continue
   155  			}
   156  			for key, update := range updateBuilder(fv.Interface(), pfv.Interface()) {
   157  				update.FieldPath = append(firestore.FieldPath{path}, update.FieldPath...)
   158  
   159  				fp := make(firestore.FieldPath, len(update.FieldPath))
   160  				copy(fp, update.FieldPath)
   161  
   162  				sp := strings.Split(key, ".")
   163  				fieldKey := strings.Join(append(fp[:len(update.FieldPath)-1], sp[len(sp)-1]), ".")
   164  
   165  				if _, ok := updateMap[fieldKey]; ok {
   166  					panic("fields with the same name cannot be used")
   167  				}
   168  
   169  				updateMap[fieldKey] = update
   170  			}
   171  			continue
   172  		}
   173  
   174  		if _, ok := updateMap[ft.Name]; ok {
   175  			panic("fields with the same name cannot be used")
   176  		}
   177  
   178  		var isValid bool
   179  		switch pfv.Kind() {
   180  		case reflect.Interface, reflect.Ptr:
   181  			if !pfv.IsNil() {
   182  				isValid = true
   183  			}
   184  		default:
   185  			if !pfv.IsZero() {
   186  				isValid = true
   187  			}
   188  		}
   189  
   190  		update := firestore.Update{FieldPath: firestore.FieldPath{path}}
   191  		if isValid {
   192  			update.Value = pfv.Interface()
   193  		}
   194  
   195  		if update.Value != nil {
   196  			updateMap[ft.Name] = update
   197  		}
   198  	}
   199  
   200  	return updateMap
   201  }