github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/gen_method/helpers.go (about)

     1  package gen_method
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  func IsSpecifyIndexSequence(fields []Field) bool {
    11  	var defaultIndexOrder, specifyIndexOrder bool
    12  	for _, field := range fields {
    13  		if field.IsSpecifyIndexOrder {
    14  			specifyIndexOrder = true
    15  		} else {
    16  			defaultIndexOrder = true
    17  		}
    18  	}
    19  
    20  	if defaultIndexOrder && specifyIndexOrder {
    21  		fmt.Printf("Some fields are specified index order but other not in same index[%s].\n", fields[0].IndexName)
    22  		os.Exit(1)
    23  	}
    24  
    25  	return specifyIndexOrder
    26  }
    27  
    28  func ParseTagSetting(str string) map[string][]string {
    29  	tags := strings.Split(str, ";")
    30  	setting := map[string][]string{}
    31  	for _, value := range tags {
    32  		v := strings.Split(value, ":")
    33  		k := strings.TrimSpace(strings.ToUpper(v[0]))
    34  		if _, ok := setting[k]; !ok {
    35  			setting[k] = make([]string, 0, 10)
    36  		}
    37  		if len(v) == 2 {
    38  			setting[k] = append(setting[k], v[1])
    39  		} else {
    40  			setting[k] = append(setting[k], k)
    41  		}
    42  	}
    43  	return setting
    44  }
    45  
    46  // ParseIndex indexName[0] -> return indexname, 0
    47  func ParseIndex(index string) (string, int, bool) {
    48  	var isSpecifyIndexOrder bool
    49  	if len(index) == 0 {
    50  		return "", -1, isSpecifyIndexOrder
    51  	}
    52  	tmpStrSlice := strings.Split(index, "[")
    53  	if len(tmpStrSlice) != 2 {
    54  		return tmpStrSlice[0], 0, isSpecifyIndexOrder
    55  	}
    56  
    57  	pos, err := strconv.ParseInt(tmpStrSlice[1][0:1], 10, 64)
    58  	if err != nil {
    59  		fmt.Printf("%s\n", err.Error())
    60  		os.Exit(1)
    61  	}
    62  
    63  	isSpecifyIndexOrder = true
    64  	return tmpStrSlice[0], int(pos), isSpecifyIndexOrder
    65  }
    66  
    67  // fetchBaseInfoOfGenFuncForNormalIndex fetch part of function name, function input param,
    68  // orm query format, orm query parameters.
    69  func fetchBaseInfoOfGenFuncForNormalIndex(indexList []Field) *BaseInfoOfGenCode {
    70  	var partFuncName, inputParam, ormQueryFormat, ormQueryParam string
    71  	for _, field := range indexList {
    72  		if len(partFuncName) == 0 {
    73  			partFuncName = field.Name
    74  			inputParam = fmt.Sprintf("db *gorm.DB, %s %s", convertFirstLetterToLower(field.Name), field.Type)
    75  			ormQueryFormat = fmt.Sprintf("%s = ?", field.DbFieldName)
    76  			ormQueryParam = fmt.Sprintf("%s", convertFirstLetterToLower(field.Name))
    77  		} else {
    78  			partFuncName += "And" + field.Name
    79  			inputParam += fmt.Sprintf(", %s %s", convertFirstLetterToLower(field.Name), field.Type)
    80  			ormQueryFormat += fmt.Sprintf(" and %s = ?", field.DbFieldName)
    81  			ormQueryParam += fmt.Sprintf(", %s", convertFirstLetterToLower(field.Name))
    82  		}
    83  	}
    84  
    85  	return &BaseInfoOfGenCode{
    86  		PartFuncName:   partFuncName,
    87  		FuncInputParam: inputParam,
    88  		OrmQueryFormat: ormQueryFormat,
    89  		OrmQueryParam:  ormQueryParam,
    90  	}
    91  }
    92  
    93  func fetchBaseInfoOfGenFuncForUniqueIndex(model *Model, indexList []Field) *BaseInfoOfGenCode {
    94  	var partFuncName, inputParam, ormQueryFormat, ormQueryParam string
    95  	inputParam = fmt.Sprintf("%s", "db *gorm.DB")
    96  	for _, field := range indexList {
    97  		if len(partFuncName) == 0 {
    98  			partFuncName = field.Name
    99  			ormQueryFormat = fmt.Sprintf("%s = ?", field.DbFieldName)
   100  			ormQueryParam = fmt.Sprintf("%s.%s", fetchUpLetter(model.Name), field.Name)
   101  		} else {
   102  			partFuncName += "And" + field.Name
   103  			ormQueryFormat += fmt.Sprintf(" and %s = ?", field.DbFieldName)
   104  			ormQueryParam += fmt.Sprintf(", %s.%s", fetchUpLetter(model.Name), field.Name)
   105  		}
   106  	}
   107  
   108  	return &BaseInfoOfGenCode{
   109  		PartFuncName:   partFuncName,
   110  		FuncInputParam: inputParam,
   111  		OrmQueryFormat: ormQueryFormat,
   112  		OrmQueryParam:  ormQueryParam,
   113  	}
   114  }
   115  
   116  func replaceUpperWithLowerAndUnderscore(src string) string {
   117  	var dst string
   118  	for index, letter := range src {
   119  		if index == 0 && isUpperLetter(letter) {
   120  			dst += fmt.Sprintf("%c", letter)
   121  		} else if isUpperLetter(letter) {
   122  			dst += fmt.Sprintf("_%c", letter)
   123  		} else {
   124  			dst += fmt.Sprintf("%c", letter)
   125  		}
   126  	}
   127  
   128  	return strings.ToLower(dst)
   129  }