summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go
blob: 8febd4a1d9e263dfcbc118542157fda7e14c677b (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package format

import (
	"reflect"
	"unsafe"

	"codeberg.org/gruf/go-xunsafe"
)

type Stringer interface{ String() string }

type Formattable interface{ Format(*State) }

var (
	// stringer type for implement checks.
	stringerType = typeof[Stringer]()

	// formattableType type for implement checks.
	formattableType = typeof[Formattable]()

	// error type for implement checks.
	errorType = typeof[error]()
)

// getMethodType returns a *possible* FormatFunc to handle case
// of a type that implements any known interface{} types, else nil.
func getMethodType(t xunsafe.TypeIter) FormatFunc {
	switch {
	case t.Type.Implements(stringerType):
		switch t.Type.Kind() {
		case reflect.Interface:
			return getInterfaceStringerType(t)
		default:
			return getConcreteStringerType(t)
		}
	case t.Type.Implements(formattableType):
		switch t.Type.Kind() {
		case reflect.Interface:
			return getInterfaceFormattableType(t)
		default:
			return getConcreteFormattableType(t)
		}
	case t.Type.Implements(errorType):
		switch t.Type.Kind() {
		case reflect.Interface:
			return getInterfaceErrorType(t)
		default:
			return getConcreteErrorType(t)
		}
	default:
		return nil
	}
}

// getInterfaceStringerType returns a FormatFunc to handle case of an interface{}
// type that implements Stringer{}, i.e. Stringer{} itself and any superset of.
func getInterfaceStringerType(t xunsafe.TypeIter) FormatFunc {
	switch t.Indirect() && !t.IfaceIndir() {
	case true:
		return with_typestr_ptrs(t, func(s *State) {
			s.P = *(*unsafe.Pointer)(s.P)
			if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
				appendNil(s)
				return
			}
			v := *(*Stringer)(s.P)
			appendString(s, v.String())
		})
	case false:
		return with_typestr_ptrs(t, func(s *State) {
			if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
				appendNil(s)
				return
			}
			v := *(*Stringer)(s.P)
			appendString(s, v.String())
		})
	default:
		panic("unreachable")
	}
}

// getConcreteStringerType returns a FormatFunc to handle case of concrete
// (i.e. non-interface{}) type that has a Stringer{} method receiver.
func getConcreteStringerType(t xunsafe.TypeIter) FormatFunc {
	itab := xunsafe.GetIfaceITab[Stringer](t.Type)
	switch t.Indirect() && !t.IfaceIndir() {
	case true:
		return with_typestr_ptrs(t, func(s *State) {
			s.P = *(*unsafe.Pointer)(s.P)
			if s.P == nil {
				appendNil(s)
				return
			}
			v := *(*Stringer)(xunsafe.PackIface(itab, s.P))
			appendString(s, v.String())
		})
	case false:
		return with_typestr_ptrs(t, func(s *State) {
			v := *(*Stringer)(xunsafe.PackIface(itab, s.P))
			appendString(s, v.String())
		})
	default:
		panic("unreachable")
	}
}

// getInterfaceFormattableType returns a FormatFunc to handle case of an interface{}
// type that implements Formattable{}, i.e. Formattable{} itself and any superset of.
func getInterfaceFormattableType(t xunsafe.TypeIter) FormatFunc {
	switch t.Indirect() && !t.IfaceIndir() {
	case true:
		return with_typestr_ptrs(t, func(s *State) {
			s.P = *(*unsafe.Pointer)(s.P)
			if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
				appendNil(s)
				return
			}
			v := *(*Formattable)(s.P)
			v.Format(s)
		})
	case false:
		return with_typestr_ptrs(t, func(s *State) {
			if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
				appendNil(s)
				return
			}
			v := *(*Formattable)(s.P)
			v.Format(s)
		})
	default:
		panic("unreachable")
	}
}

// getConcreteFormattableType returns a FormatFunc to handle case of concrete
// (i.e. non-interface{}) type that has a Formattable{} method receiver.
func getConcreteFormattableType(t xunsafe.TypeIter) FormatFunc {
	itab := xunsafe.GetIfaceITab[Formattable](t.Type)
	switch t.Indirect() && !t.IfaceIndir() {
	case true:
		return with_typestr_ptrs(t, func(s *State) {
			s.P = *(*unsafe.Pointer)(s.P)
			if s.P == nil {
				appendNil(s)
				return
			}
			v := *(*Formattable)(xunsafe.PackIface(itab, s.P))
			v.Format(s)
		})
	case false:
		return with_typestr_ptrs(t, func(s *State) {
			v := *(*Formattable)(xunsafe.PackIface(itab, s.P))
			v.Format(s)
		})
	default:
		panic("unreachable")
	}
}

// getInterfaceErrorType returns a FormatFunc to handle case of an interface{}
// type that implements error{}, i.e. error{} itself and any superset of.
func getInterfaceErrorType(t xunsafe.TypeIter) FormatFunc {
	switch t.Indirect() && !t.IfaceIndir() {
	case true:
		return with_typestr_ptrs(t, func(s *State) {
			s.P = *(*unsafe.Pointer)(s.P)
			if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
				appendNil(s)
				return
			}
			v := *(*error)(s.P)
			appendString(s, v.Error())
		})
	case false:
		return with_typestr_ptrs(t, func(s *State) {
			if s.P == nil || (*xunsafe.Abi_NonEmptyInterface)(s.P).Data == nil {
				appendNil(s)
				return
			}
			v := *(*error)(s.P)
			appendString(s, v.Error())
		})
	default:
		panic("unreachable")
	}
}

// getConcreteErrorType returns a FormatFunc to handle case of concrete
// (i.e. non-interface{}) type that has an error{} method receiver.
func getConcreteErrorType(t xunsafe.TypeIter) FormatFunc {
	itab := xunsafe.GetIfaceITab[error](t.Type)
	switch t.Indirect() && !t.IfaceIndir() {
	case true:
		return with_typestr_ptrs(t, func(s *State) {
			s.P = *(*unsafe.Pointer)(s.P)
			if s.P == nil {
				appendNil(s)
				return
			}
			v := *(*error)(xunsafe.PackIface(itab, s.P))
			appendString(s, v.Error())
		})
	case false:
		return with_typestr_ptrs(t, func(s *State) {
			v := *(*error)(xunsafe.PackIface(itab, s.P))
			appendString(s, v.Error())
		})
	default:
		panic("unreachable")
	}
}