-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
123 lines (111 loc) · 3.13 KB
/
Copy pathutil.go
File metadata and controls
123 lines (111 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package morm
import (
"errors"
"reflect"
"strings"
)
// getModelType returns the reflect.Type of the model pointed to by the input pointer.
//
// Parameters:
// - value: A pointer to the model instance.
//
// Returns:
// - reflect.Type: The reflect.Type of the model.
// - error: An error if the input is not a pointer.
//
// Example:
//
// var user User
// modelType, err := getModelType(&user)
// if err != nil {
// // Handle error
// }
//
// This function is useful for obtaining the reflect.Type of a model from a pointer.
func getModelType(value interface{}) (reflect.Type, error) {
resultType := reflect.TypeOf(value)
if resultType.Kind() != reflect.Ptr {
return nil, errors.New("result must be a pointer")
}
modelType := resultType.Elem()
return modelType, nil
}
// getFirstStringAfterSplit returns the first string after splitting the input string by commas.
//
// Parameters:
// - input: The input string to be split.
//
// Returns:
// - string: The first string after splitting.
//
// Example:
//
// input := "apple, orange, banana"
// firstString := getFirstStringAfterSplit(input)
//
// This function is useful for extracting the first string from a comma-separated list.
func getFirstStringAfterSplit(input string) string {
splitStrings := strings.Split(input, ",")
if len(splitStrings) > 0 {
return splitStrings[0]
}
return ""
}
// getTags retrieves specific tags associated with a field in a struct.
//
// Parameters:
// - modelType: The reflect.Type of the struct.
// - field: The field for which tags are to be retrieved.
//
// Returns:
// - map[string]string: A map of tag names to tag values.
// - error: An error if the field is not found.
//
// Example:
//
// var user User
// tags, err := getTags(reflect.TypeOf(user), "Name")
// if err != nil {
// // Handle error
// }
//
// This function is useful for obtaining specific tags associated with a struct field.
func getTags(modelType reflect.Type, field string) (map[string]string, error) {
fieldTags := make(map[string]string)
for i := 0; i < modelType.NumField(); i++ {
structField := modelType.Field(i)
if structField.Name == field {
localFieldTag := structField.Tag.Get("localField")
foreignFieldTag := structField.Tag.Get("foreignField")
justOneTag := structField.Tag.Get("justOne")
countTag := structField.Tag.Get("count")
jsonTag := structField.Tag.Get("json")
fieldTags["localField"] = localFieldTag
fieldTags["foreignField"] = foreignFieldTag
fieldTags["justOne"] = justOneTag
fieldTags["count"] = countTag
fieldTags["json"] = jsonTag
return fieldTags, nil
}
}
return nil, errors.New("field not found")
}
// removeBrackets removes "[" and "]" from the input string.
//
// Parameters:
// - input: The input string containing "[" and "]".
//
// Returns:
// - string: The input string with "[" and "]" removed.
//
// Example:
//
// input := "[apple]"
// result := removeBrackets(input)
//
// This function is useful for cleaning up strings that contain square brackets.
func removeBrackets(input string) string {
// Remove "[" and "]"
result := strings.ReplaceAll(strings.ReplaceAll(input, "[", ""), "]", "")
return result
}