From cb366a630b084d9e2f24ca84a34d04fc8b2777d8 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 28 Oct 2024 02:07:40 +0300 Subject: [PATCH] Better helpers for properties --- pachca.go | 100 ++++++++++++++++++++++++++++++++++++------------- pachca_test.go | 50 +++++++++++++++---------- 2 files changed, 106 insertions(+), 44 deletions(-) diff --git a/pachca.go b/pachca.go index 93ff8d6..63f42c3 100644 --- a/pachca.go +++ b/pachca.go @@ -14,6 +14,7 @@ import ( "mime/multipart" "os" "regexp" + "slices" "strconv" "strings" "time" @@ -396,6 +397,7 @@ var ( ErrNilUserRequest = errors.New("User requests is nil") ErrNilChatRequest = errors.New("Chat requests is nil") ErrNilMessageRequest = errors.New("Message requests is nil") + ErrNilProperty = errors.New("Property requests is nil") ErrEmptyToken = errors.New("Token is empty") ErrEmptyTag = errors.New("Group tag is empty") ErrEmptyMessage = errors.New("Message text is empty") @@ -1565,7 +1567,7 @@ func (c *Client) UploadFile(file string) (*File, error) { // ////////////////////////////////////////////////////////////////////////////////// // -// Get returns value of custom property with given name +// Get returns custom property with given name func (p Properties) Get(name string) *Property { for _, pp := range p { if pp.Name == name { @@ -1576,47 +1578,95 @@ func (p Properties) Get(name string) *Property { return nil } -// GetD returns value of custom property with given name as string -func (p Properties) GetS(name string) string { - pp := p.Get(name) +// GetAny returns first found property with one of given names +func (p Properties) GetAny(name ...string) *Property { + for _, pp := range p { + if slices.Contains(name, pp.Name) { + return pp + } + } - if pp == nil || pp.Value == "" { - return "" + return nil +} + +// Names returns slice with properties names +func (p Properties) Names() []string { + var result []string + + for _, pp := range p { + result = append(result, pp.Name) } - return pp.Value + return result +} + +// IsText returns true if property has text type +func (p *Property) IsText() bool { + return p != nil && p.Type == PROP_TYPE_TEXT } -// GetD returns value of custom property with given name as date -func (p Properties) GetD(name string) (time.Time, error) { - pp := p.Get(name) +// IsLink returns true if property has URL type +func (p *Property) IsLink() bool { + return p != nil && p.Type == PROP_TYPE_LINK +} + +// IsDate returns true if property has date type +func (p *Property) IsDate() bool { + return p != nil && p.Type == PROP_TYPE_DATE +} +// IsNumber returns true if property has number type +func (p *Property) IsNumber() bool { + return p != nil && p.Type == PROP_TYPE_NUMBER +} + +// String returns property value +func (p *Property) String() string { + if p == nil { + return "" + } + + return p.Value +} + +// ToDate tries to convert property value to date +func (p *Property) ToDate() (time.Time, error) { switch { - case pp == nil: - return time.Time{}, fmt.Errorf("There is not property with name %q", name) - case pp.Value == "": + case p == nil: + return time.Time{}, ErrNilProperty + case p.Value == "": return time.Time{}, nil - case pp.Type != PROP_TYPE_DATE: - return time.Time{}, fmt.Errorf("Invalid property type for GetD (%s)", pp.Type) + case p.Type != PROP_TYPE_DATE: + return time.Time{}, fmt.Errorf("Invalid property type for date (%s)", p.Type) } - return parseDate(pp.Value) + return parseDate(p.Value) } -// GetD returns value of custom property with given name as int -func (p Properties) GetI(name string) (int, error) { - pp := p.Get(name) +// Date returns property value as date +func (p *Property) Date() time.Time { + d, _ := p.ToDate() + return d +} +// ToInt tries to convert property value to int +func (p *Property) ToInt() (int, error) { switch { - case pp == nil: - return 0, fmt.Errorf("There is not property with name %q", name) - case pp.Value == "": + case p == nil: + return 0, ErrNilProperty + case p.Value == "": return 0, nil - case pp.Type != PROP_TYPE_NUMBER: - return 0, fmt.Errorf("Invalid property type for GetI (%s)", pp.Type) + case p.Type != PROP_TYPE_NUMBER: + return 0, fmt.Errorf("Invalid property type for date (%s)", p.Type) } - return strconv.Atoi(pp.Value) + return strconv.Atoi(p.Value) +} + +// Int returns property value as int +func (p *Property) Int() int { + i, _ := p.ToInt() + return i } // FullName returns user full name diff --git a/pachca_test.go b/pachca_test.go index 86814bd..b805ecc 100644 --- a/pachca_test.go +++ b/pachca_test.go @@ -304,7 +304,7 @@ func (s *PachcaSuite) TestPropertiesHelpers(c *C) { {ID: 1, Type: PROP_TYPE_DATE, Name: "test1", Value: "2024-08-08T09:11:50.368Z"}, {ID: 2, Type: PROP_TYPE_LINK, Name: "test2", Value: "https://domain.com"}, {ID: 3, Type: PROP_TYPE_NUMBER, Name: "test3", Value: "314"}, - {ID: 4, Type: PROP_TYPE_TEXT, Name: "test4", Value: "Test1"}, + {ID: 4, Type: PROP_TYPE_TEXT, Name: "test4", Value: "Test"}, {ID: 5, Type: PROP_TYPE_NUMBER, Name: "test5", Value: ""}, {ID: 6, Type: PROP_TYPE_DATE, Name: "test6", Value: ""}, } @@ -312,30 +312,42 @@ func (s *PachcaSuite) TestPropertiesHelpers(c *C) { c.Assert(p.Get("test"), IsNil) c.Assert(p.Get("test1"), NotNil) - c.Assert(p.GetS("test2"), Equals, "https://domain.com") - c.Assert(p.GetS("test4"), Equals, "Test1") - c.Assert(p.GetS("test100"), Equals, "") + c.Assert(p.GetAny("abcd", "test100", "test"), IsNil) + c.Assert(p.GetAny("abcd", "test4", "test").Name, Equals, "test4") - i, err := p.GetI("test3") - c.Assert(err, IsNil) - c.Assert(i, Equals, 314) - i, err = p.GetI("test5") + c.Assert(p.Names(), DeepEquals, []string{"test1", "test2", "test3", "test4", "test5", "test6"}) + + c.Assert(p.Get("test4").IsText(), Equals, true) + c.Assert(p.Get("test2").IsLink(), Equals, true) + c.Assert(p.Get("test1").IsDate(), Equals, true) + c.Assert(p.Get("test3").IsNumber(), Equals, true) + + c.Assert(p.Get("test2").String(), Equals, "https://domain.com") + c.Assert(p.Get("test4").String(), Equals, "Test") + c.Assert(p.Get("test100").String(), Equals, "") + + c.Assert(p.Get("test1").Date().IsZero(), Equals, false) + c.Assert(p.Get("test2").Date().IsZero(), Equals, true) + + c.Assert(p.Get("test3").Int(), Equals, 314) + c.Assert(p.Get("test2").Int(), Equals, 0) + + _, err := p.Get("test6").ToDate() c.Assert(err, IsNil) - c.Assert(i, Equals, 0) - _, err = p.GetI("test4") - c.Assert(err, NotNil) - _, err = p.GetI("test100") + _, err = p.Get("test2").ToDate() c.Assert(err, NotNil) - d, err := p.GetD("test1") - c.Assert(err, IsNil) - c.Assert(d.IsZero(), Equals, false) - d, err = p.GetD("test6") + _, err = p.Get("test5").ToInt() c.Assert(err, IsNil) - _, err = p.GetD("test4") - c.Assert(err, NotNil) - _, err = p.GetD("test100") + _, err = p.Get("test2").ToInt() c.Assert(err, NotNil) + + var pp *Property + + _, err = pp.ToDate() + c.Assert(err, Equals, ErrNilProperty) + _, err = pp.ToInt() + c.Assert(err, Equals, ErrNilProperty) } func (s *PachcaSuite) TestUsersHelpers(c *C) {