File: helper.go

package info (click to toggle)
golang-github-francoispqt-gojay 1.2.13-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 86
file content (101 lines) | stat: -rw-r--r-- 2,227 bytes parent folder | download
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
package codegen

import (
	"github.com/viant/toolbox"
	"reflect"
	"strings"
	"sort"
)

func firstLetterToUppercase(text string) string {
	return strings.ToUpper(string(text[0:1])) + string(text[1:])
}

func firstLetterToLowercase(text string) string {
	return strings.ToLower(string(text[0:1])) + string(text[1:])
}

func extractReceiverAlias(structType string) string {
	var result = string(structType[0])
	for i := len(structType) - 1; i > 0; i-- {
		aChar := string(structType[i])
		lowerChar := strings.ToLower(aChar)
		if lowerChar != aChar {
			result = lowerChar
			break
		}
	}
	return strings.ToLower(result)
}

func getTagOptions(tag, key string) []string {
	if tag == "" {
		return nil
	}
	var structTag = reflect.StructTag(strings.Replace(tag, "`", "", len(tag)))
	options, ok := structTag.Lookup(key)
	if !ok {
		return nil
	}
	return strings.Split(options, ",")
}

func getSliceHelperTypeName(typeName string, isPointer bool) string {
	if typeName == "" {
		return ""
	}

	var pluralName = firstLetterToUppercase(typeName) + "s"
	if isPointer {
		pluralName += "Ptr"
	}
	return strings.Replace(pluralName, ".", "", -1)
}

func isSkipable(options *Options, field *toolbox.FieldInfo) bool {
	if options := getTagOptions(field.Tag, options.TagName); len(options) > 0 {
		for _, candidate := range options {
			if candidate == "-" {
				return true
			}
		}
	}
	return false
}

func wrapperIfNeeded(text, wrappingChar string) string {
	if strings.HasPrefix(text, wrappingChar) {
		return text
	}
	return wrappingChar + text + wrappingChar
}

func getPoolName(typeName string) string {
	typeName = strings.Replace(typeName, "*", "", 1)
	return strings.Replace(typeName+"Pool", ".", "", -1)
}

func getJSONKey(options *Options, field *toolbox.FieldInfo) string {
	var key = field.Name
	if field.Tag != "" {
		if options := getTagOptions(field.Tag, options.TagName); len(options) > 0 {
			key = options[0]
		}
	}
	return key
}

func normalizeTypeName(typeName string) string {
	return strings.Replace(typeName, "*", "", strings.Count(typeName, "*"))
}

func sortedKeys(m map[string]string) ([]string) {
	keys := make([]string, len(m))
	i := 0
	for k := range m {
		keys[i] = k
		i++
	}
	sort.Strings(keys)
	return keys
}