feat(ls): output available fields with - filter flag

This commit is contained in:
iyear 2023-05-22 21:16:47 +08:00
parent d0c9aa2884
commit ea4c9f990a
4 changed files with 41 additions and 24 deletions

View File

@ -10,7 +10,6 @@ import (
"github.com/gotd/td/telegram/query"
"github.com/gotd/td/tg"
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/texpr"
"github.com/iyear/tdl/pkg/utils"
"github.com/mattn/go-runewidth"
@ -21,16 +20,16 @@ import (
)
type dialog struct {
ID int64 `json:"id"`
VisibleName string `json:"visible_name,omitempty"`
Username string `json:"username,omitempty"`
Type string `json:"type"`
Topics []topic `json:"topics,omitempty"`
ID int64 `json:"id" comment:"ID of dialog"`
Type string `json:"type" comment:"Type of dialog. Can be 'user', 'channel' or 'group'"`
VisibleName string `json:"visible_name,omitempty" comment:"Title of channel and group, first and last name of user. If empty, output '-'"`
Username string `json:"username,omitempty" comment:"Username of dialog. If empty, output '-'"`
Topics []topic `json:"topics,omitempty" comment:"Topics of dialog. If not set, output '-'"`
}
type topic struct {
ID int `json:"id"`
Title string `json:"title"`
ID int `json:"id" comment:"ID of topic"`
Title string `json:"title" comment:"Title of topic"`
}
type Output string
@ -40,6 +39,14 @@ var (
OutputJSON Output = "json"
)
// External designation, different from Telegram mtproto
const (
DialogGroup = "group"
DialogPrivate = "private"
DialogChannel = "channel"
DialogUnknown = "unknown"
)
type ListOptions struct {
Output string
Filter string
@ -50,6 +57,17 @@ func List(ctx context.Context, opts ListOptions) error {
runewidth.EastAsianWidth = false
runewidth.DefaultCondition.EastAsianWidth = false
// output available fields
if opts.Filter == "-" {
fg := texpr.NewFieldsGetter(nil)
fields, err := fg.Walk(&dialog{})
if err != nil {
return fmt.Errorf("failed to walk fields: %w", err)
}
fmt.Println(fg.Sprint(fields, true))
return nil
}
// compile filter
filter, err := texpr.Compile(opts.Filter)
if err != nil {
@ -176,7 +194,7 @@ func processUser(id int64, entities peer.Entities) *dialog {
ID: u.ID,
VisibleName: visibleName(u.FirstName, u.LastName),
Username: u.Username,
Type: consts.DialogPrivate,
Type: DialogPrivate,
Topics: nil,
}
}
@ -196,11 +214,11 @@ func processChannel(ctx context.Context, api *tg.Client, id int64, entities peer
// channel type
switch {
case c.Broadcast:
d.Type = consts.DialogChannel
d.Type = DialogChannel
case c.Megagroup, c.Gigagroup:
d.Type = consts.DialogGroup
d.Type = DialogGroup
default:
d.Type = consts.DialogUnknown
d.Type = DialogUnknown
}
if c.Forum {
@ -238,7 +256,7 @@ func processChat(id int64, entities peer.Entities) *dialog {
ID: c.ID,
VisibleName: c.Title,
Username: "-",
Type: consts.DialogGroup,
Type: DialogGroup,
Topics: nil,
}
}

View File

@ -31,12 +31,4 @@ var (
}
)
// External designation, different from Telegram mtproto
const (
DialogGroup = "group"
DialogPrivate = "private"
DialogChannel = "channel"
DialogUnknown = "unknown"
)
const FileMaxSize = 4000 * 1024 * 1024

View File

@ -76,7 +76,7 @@ func (f *FieldsGetter) walk(v reflect.Type, field *Field, fields *[]*Field) {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.String, reflect.Bool, reflect.Map, reflect.Array, reflect.Slice:
reflect.Float32, reflect.Float64, reflect.String, reflect.Bool, reflect.Map:
field.Type = v
*fields = append(*fields, field)
@ -93,6 +93,9 @@ func (f *FieldsGetter) walk(v reflect.Type, field *Field, fields *[]*Field) {
Comment: fd.Tag.Get(f.opts.tagName),
}, fields)
}
case reflect.Array, reflect.Slice:
field.Path[len(field.Path)-1] += "[]" // note this is an array or slice
f.walk(v.Elem(), field, fields)
case reflect.Pointer:
f.walk(v.Elem(), field, fields)
}

View File

@ -19,6 +19,9 @@ func TestFieldsGetter(t *testing.T) {
F9 map[string]string `comment:"f9 comment"`
F10 [3]bool `comment:"f10 comment"`
F11 []float32 `comment:"f11 comment"`
F12 []struct {
F13 int `comment:"f13 comment"`
}
}
}
}
@ -35,8 +38,9 @@ F4.F5: uint8 # f5 comment
F4.F6: uint16 # f6 comment
F4.F7.F8: string # f8 comment
F4.F7.F9: map[string]string # f9 comment
F4.F7.F10: [3]bool # f10 comment
F4.F7.F11: []float32 # f11 comment
F4.F7.F10[]: bool # f10 comment
F4.F7.F11[]: float32 # f11 comment
F4.F7.F12[].F13: int # f13 comment
`
assert.Equal(t, expected, fg.Sprint(fields, false))
}