| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package ultraviolet
- import (
- "fmt"
- "reflect"
- )
- type SQ struct {
- value reflect.Value
- }
- func NewSQ(value any) *SQ {
- return &SQ{
- value: reflect.ValueOf(value).Elem(),
- }
- }
- func newSQInternal(value reflect.Value) *SQ {
- return &SQ{
- value: value,
- }
- }
- func (this *SQ) Field(name string) *SQ {
- if this.value.Kind() != reflect.Struct {
- return nil
- }
- return newSQInternal(
- this.value.FieldByName(name),
- )
- }
- func (this *SQ) Length() int {
- return this.value.Len()
- }
- func (this *SQ) Index(index int) *SQ {
- if this.value.Kind() != reflect.Slice {
- return nil
- }
- return newSQInternal(
- this.value.Index(index),
- )
- }
- func (this *SQ) Keys() []string {
- result := []string{}
- for _, key := range this.value.MapKeys() {
- result = append(result, key.String())
- }
- return result
- }
- func (this *SQ) Key(key string) *SQ {
- if this.value.Kind() != reflect.Map {
- return nil
- }
- return newSQInternal(
- this.value.MapIndex(reflect.ValueOf(key)),
- )
- }
- func (this *SQ) Value() any {
- if this.value.Kind() == reflect.Int {
- return fmt.Sprintf("%d", this.value.Int())
- }
- return this.value.String()
- }
- func (this *SQ) ValueInt() any {
- return this.value.Int()
- }
- func (this *SQ) ValueString() any {
- return this.value.String()
- }
|