🐛 Replace mutually exclusive bool flags with single --for enum in subscription

related issue #215
This commit is contained in:
OpenWaygate committed 2026-06-27 18:36:10 +08:00
1 parent 1dc1537711
commit 9cf757851c
4 files changed
+30 -100

No files matched your search

+5 -16
View File
@@ -48,14 +48,9 @@ var listInSchema = &jsonschema.Schema{
Default: json.RawMessage("5"),
Minimum: new(float64(0)),
},
"mine": {
Type: "boolean", Description: mineUsage,
},
"my_recent_subscribers": {
Type: "boolean", Description: mrsUsage,
},
"my_subscribers": {
Type: "boolean", Description: msUsage,
"for": {
Type: "string", Description: forUsage,
Enum: []any{"mine", "myRecentSubscribers", "mySubscribers"},
},
"on_behalf_of_content_owner": {
Type: "string",
@@ -106,11 +101,7 @@ func init() {
listCmd.Flags().StringVarP(&channelId, "channelId", "c", "", listCidUsage)
listCmd.Flags().StringVarP(&forChannelId, "forChannelId", "C", "", fcidUsage)
listCmd.Flags().Int64VarP(&maxResults, "maxResults", "n", 5, pkg.MRUsage)
listCmd.Flags().BoolVarP(mine, "mine", "M", true, mineUsage)
listCmd.Flags().BoolVarP(
myRecentSubscribers, "myRecentSubscribers", "R", false, mrsUsage,
)
listCmd.Flags().BoolVarP(mySubscribers, "mySubscribers", "S", false, msUsage)
listCmd.Flags().StringVarP(&subscriptionFor, "for", "F", "", forUsage)
listCmd.Flags().StringVarP(
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", pkg.OBOCOUsage,
)
@@ -137,9 +128,7 @@ var listCmd = &cobra.Command{
subscription.WithChannelId(channelId),
subscription.WithForChannelId(forChannelId),
subscription.WithMaxResults(maxResults),
subscription.WithMine(mine),
subscription.WithMyRecentSubscribers(myRecentSubscribers),
subscription.WithMySubscribers(mySubscribers),
subscription.WithFor(subscriptionFor),
subscription.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
subscription.WithOnBehalfOfContentOwnerChannel(onBehalfOfContentOwnerChannel),
subscription.WithOrder(order),
+2 -15
View File
@@ -5,7 +5,6 @@ package subscription
import (
"github.com/eat-pray-ai/yutu/cmd"
"github.com/eat-pray-ai/yutu/pkg/utils"
"github.com/spf13/cobra"
)
@@ -15,9 +14,7 @@ const (
scidUsage = "Subscriber's channel id"
descUsage = "Description of the subscription"
fcidUsage = "Return the subscriptions to the subset of these channels that the authenticated user is subscribed to"
mineUsage = "Return the subscriptions of the authenticated user"
mrsUsage = "true|false|null"
msUsage = "Return the subscribers of the given channel owner"
forUsage = "mine|myRecentSubscribers|mySubscribers"
orderUsage = "subscriptionOrderUnspecified|relevance|unread|alphabetical"
titleUsage = "Title of the subscription"
)
@@ -29,9 +26,7 @@ var (
channelId string
forChannelId string
maxResults int64
mine = new(false)
myRecentSubscribers = new(false)
mySubscribers = new(false)
subscriptionFor string
onBehalfOfContentOwner string
onBehalfOfContentOwnerChannel string
order string
@@ -43,14 +38,6 @@ var subscriptionCmd = &cobra.Command{
Use: "subscription",
Short: short,
Long: long,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
boolMap := map[string]**bool{
"mine": &mine,
"myRecentSubscribers": &myRecentSubscribers,
"mySubscribers": &mySubscribers,
}
utils.ResetBool(boolMap, cmd.Flags())
},
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
+10 -31
View File
@@ -24,9 +24,7 @@ type Subscription struct {
SubscriberChannelId string `yaml:"subscriber_channel_id" json:"subscriber_channel_id,omitempty"`
Description string `yaml:"description" json:"description,omitempty"`
ForChannelId string `yaml:"for_channel_id" json:"for_channel_id,omitempty"`
Mine *bool `yaml:"mine" json:"mine,omitempty"`
MyRecentSubscribers *bool `yaml:"my_recent_subscribers" json:"my_recent_subscribers,omitempty"`
MySubscribers *bool `yaml:"my_subscribers" json:"my_subscribers,omitempty"`
For string `yaml:"for" json:"for,omitempty"`
Order string `yaml:"order" json:"order,omitempty"`
Title string `yaml:"title" json:"title,omitempty"`
@@ -64,14 +62,13 @@ func (s *Subscription) Get() ([]*youtube.Subscription, error) {
if s.ForChannelId != "" {
call = call.ForChannelId(s.ForChannelId)
}
if s.Mine != nil {
call = call.Mine(*s.Mine)
}
if s.MyRecentSubscribers != nil {
call = call.MyRecentSubscribers(*s.MyRecentSubscribers)
}
if s.MySubscribers != nil {
call = call.MySubscribers(*s.MySubscribers)
switch s.For {
case "mine":
call = call.Mine(true)
case "myRecentSubscribers":
call = call.MyRecentSubscribers(true)
case "mySubscribers":
call = call.MySubscribers(true)
}
if s.OnBehalfOfContentOwner != "" {
call = call.OnBehalfOfContentOwner(s.OnBehalfOfContentOwner)
@@ -177,27 +174,9 @@ func WithForChannelId(forChannelId string) Option {
}
}
func WithMine(mine *bool) Option {
func WithFor(f string) Option {
return func(s *Subscription) {
if mine != nil {
s.Mine = mine
}
}
}
func WithMyRecentSubscribers(myRecentSubscribers *bool) Option {
return func(s *Subscription) {
if myRecentSubscribers != nil {
s.MyRecentSubscribers = myRecentSubscribers
}
}
}
func WithMySubscribers(mySubscribers *bool) Option {
return func(s *Subscription) {
if mySubscribers != nil {
s.MySubscribers = mySubscribers
}
s.For = f
}
}
+13 -38
View File
@@ -21,12 +21,6 @@ func TestNewSubscription(t *testing.T) {
opts []Option
}
mineTrue := true
mineFalse := false
myRecentSubscribersTrue := true
myRecentSubscribersFalse := false
mySubscribersTrue := true
mySubscribersFalse := false
svc := &youtube.Service{}
tests := []struct {
@@ -44,9 +38,7 @@ func TestNewSubscription(t *testing.T) {
WithChannelId("channel123"),
WithForChannelId("forChannel123"),
WithMaxResults(50),
WithMine(&mineTrue),
WithMyRecentSubscribers(&myRecentSubscribersTrue),
WithMySubscribers(&mySubscribersTrue),
WithFor("mine"),
WithOnBehalfOfContentOwner("owner123"),
WithOnBehalfOfContentOwnerChannel("ownerChannel123"),
WithOrder("relevance"),
@@ -69,9 +61,7 @@ func TestNewSubscription(t *testing.T) {
SubscriberChannelId: "subscriber123",
Description: "Test subscription description",
ForChannelId: "forChannel123",
Mine: &mineTrue,
MyRecentSubscribers: &myRecentSubscribersTrue,
MySubscribers: &mySubscribersTrue,
For: "mine",
OnBehalfOfContentOwnerChannel: "ownerChannel123",
Order: "relevance",
Title: "Test Subscription",
@@ -85,30 +75,24 @@ func TestNewSubscription(t *testing.T) {
want: &Subscription{Fields: common.Fields{}},
},
{
name: "with nil boolean options",
name: "with empty for option",
args: args{
opts: []Option{
WithMine(nil),
WithMyRecentSubscribers(nil),
WithMySubscribers(nil),
WithFor(""),
},
},
want: &Subscription{Fields: common.Fields{}},
},
{
name: "with false boolean options",
name: "with for mySubscribers",
args: args{
opts: []Option{
WithMine(&mineFalse),
WithMyRecentSubscribers(&myRecentSubscribersFalse),
WithMySubscribers(&mySubscribersFalse),
WithFor("mySubscribers"),
},
},
want: &Subscription{
Fields: common.Fields{},
Mine: &mineFalse,
MyRecentSubscribers: &myRecentSubscribersFalse,
MySubscribers: &mySubscribersFalse,
Fields: common.Fields{},
For: "mySubscribers",
},
},
{
@@ -168,7 +152,7 @@ func TestNewSubscription(t *testing.T) {
WithTitle("My Subscription"),
WithMaxResults(25),
WithOrder("alphabetical"),
WithMine(&mineTrue),
WithFor("mine"),
},
},
want: &Subscription{
@@ -178,7 +162,7 @@ func TestNewSubscription(t *testing.T) {
},
Title: "My Subscription",
Order: "alphabetical",
Mine: &mineTrue,
For: "mine",
},
},
}
@@ -238,10 +222,7 @@ func TestSubscription_Get(t *testing.T) {
{
name: "get subscription mine",
opts: []Option{
func(s *Subscription) {
b := true
s.Mine = &b
},
WithFor("mine"),
WithMaxResults(1),
},
verify: func(r *http.Request) {
@@ -255,10 +236,7 @@ func TestSubscription_Get(t *testing.T) {
{
name: "get subscription myRecentSubscribers",
opts: []Option{
func(s *Subscription) {
b := true
s.MyRecentSubscribers = &b
},
WithFor("myRecentSubscribers"),
WithMaxResults(1),
},
verify: func(r *http.Request) {
@@ -275,10 +253,7 @@ func TestSubscription_Get(t *testing.T) {
{
name: "get subscription mySubscribers",
opts: []Option{
func(s *Subscription) {
b := true
s.MySubscribers = &b
},
WithFor("mySubscribers"),
WithMaxResults(1),
},
verify: func(r *http.Request) {