Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.0.2 #2

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 75 additions & 25 deletions pachca.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"mime/multipart"
"os"
"regexp"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
50 changes: 31 additions & 19 deletions pachca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,38 +304,50 @@ 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: ""},
}

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) {
Expand Down