github.com/expr-lang/expr@v1.16.9/test/playground/env.go (about) 1 package playground 2 3 import ( 4 "time" 5 ) 6 7 type UserProfile struct { 8 Birthday time.Time 9 Biography string 10 Website string 11 } 12 13 func (u UserProfile) Age() int { 14 return time.Now().Year() - u.Birthday.Year() 15 } 16 17 type Author struct { 18 ID int 19 FirstName string 20 LastName string 21 Email string 22 Profile UserProfile 23 } 24 25 func (a Author) FullName() string { 26 return a.FirstName + " " + a.LastName 27 } 28 29 func (a Author) IsAdmin() bool { 30 return a.ID == 1 31 } 32 33 type Post struct { 34 ID int 35 Title string 36 Content string 37 PublishDate time.Time 38 Author Author 39 Comments []Comment 40 Tags []string 41 Likes int 42 } 43 44 func (p Post) Published() bool { 45 return !p.PublishDate.IsZero() 46 } 47 48 func (p Post) After(date time.Time) bool { 49 return p.PublishDate.After(date) 50 } 51 52 func (p Post) Before(date time.Time) bool { 53 return p.PublishDate.Before(date) 54 } 55 56 func (p Post) Compare(other Post) int { 57 if p.PublishDate.Before(other.PublishDate) { 58 return -1 59 } else if p.PublishDate.After(other.PublishDate) { 60 return 1 61 } 62 return 0 63 } 64 65 func (p Post) Equal(other Post) bool { 66 return p.Compare(other) == 0 67 } 68 69 func (p Post) IsZero() bool { 70 return p.ID == 0 && p.Title == "" && p.Content == "" && p.PublishDate.IsZero() 71 } 72 73 type Comment struct { 74 ID int 75 AuthorName string 76 Content string 77 CommentDate time.Time 78 Upvotes int 79 } 80 81 func (c Comment) Upvoted() bool { 82 return c.Upvotes > 0 83 } 84 85 func (c Comment) AuthorEmail() string { 86 return c.AuthorName + "@example.com" 87 } 88 89 type Blog struct { 90 Posts []Post 91 Authors map[int]Author 92 TotalViews int 93 TotalPosts int 94 TotalLikes int 95 } 96 97 func (b Blog) RecentPosts() []Post { 98 var posts []Post 99 for _, post := range b.Posts { 100 if post.Published() { 101 posts = append(posts, post) 102 } 103 } 104 return posts 105 } 106 107 func (b Blog) PopularPosts() []Post { 108 var posts []Post 109 for _, post := range b.Posts { 110 if post.Likes > 150 { 111 posts = append(posts, post) 112 } 113 } 114 return posts 115 } 116 117 func (b Blog) TotalUpvotes() int { 118 var upvotes int 119 for _, post := range b.Posts { 120 for _, comment := range post.Comments { 121 upvotes += comment.Upvotes 122 } 123 } 124 return upvotes 125 } 126 127 func (b Blog) TotalComments() int { 128 var comments int 129 for _, post := range b.Posts { 130 comments += len(post.Comments) 131 } 132 return comments 133 } 134 135 func (Blog) Add(a, b float64) float64 { 136 return a + b 137 } 138 139 func (Blog) Sub(a, b float64) float64 { 140 return a - b 141 } 142 143 func (Blog) Title(post Post) string { 144 return post.Title 145 } 146 147 func (Blog) HasTag(post Post, tag string) bool { 148 for _, t := range post.Tags { 149 if t == tag { 150 return true 151 } 152 } 153 return false 154 } 155 156 func (Blog) IsAdmin(author Author) bool { 157 return author.IsAdmin() 158 } 159 160 func (Blog) IsZero(post Post) bool { 161 return post.IsZero() 162 } 163 164 func (Blog) WithID(posts []Post, id int) Post { 165 for _, post := range posts { 166 if post.ID == id { 167 return post 168 } 169 } 170 return Post{} 171 }