feed.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model
  5. import (
  6. "fmt"
  7. "reflect"
  8. "time"
  9. )
  10. // Feed represents a feed in the database.
  11. type Feed struct {
  12. ID int64 `json:"id"`
  13. UserID int64 `json:"user_id"`
  14. FeedURL string `json:"feed_url"`
  15. SiteURL string `json:"site_url"`
  16. Title string `json:"title"`
  17. CheckedAt time.Time `json:"checked_at,omitempty"`
  18. EtagHeader string `json:"etag_header,omitempty"`
  19. LastModifiedHeader string `json:"last_modified_header,omitempty"`
  20. ParsingErrorMsg string `json:"parsing_error_message,omitempty"`
  21. ParsingErrorCount int `json:"parsing_error_count,omitempty"`
  22. ScraperRules string `json:"scraper_rules"`
  23. RewriteRules string `json:"rewrite_rules"`
  24. Crawler bool `json:"crawler"`
  25. Category *Category `json:"category,omitempty"`
  26. Entries Entries `json:"entries,omitempty"`
  27. Icon *FeedIcon `json:"icon,omitempty"`
  28. }
  29. func (f *Feed) String() string {
  30. return fmt.Sprintf("ID=%d, UserID=%d, FeedURL=%s, SiteURL=%s, Title=%s, Category={%s}",
  31. f.ID,
  32. f.UserID,
  33. f.FeedURL,
  34. f.SiteURL,
  35. f.Title,
  36. f.Category,
  37. )
  38. }
  39. // Merge combine src to the current struct
  40. func (f *Feed) Merge(src *Feed) {
  41. src.ID = f.ID
  42. src.UserID = f.UserID
  43. new := reflect.ValueOf(src).Elem()
  44. for i := 0; i < new.NumField(); i++ {
  45. field := new.Field(i)
  46. switch field.Interface().(type) {
  47. case int64:
  48. value := field.Int()
  49. if value != 0 {
  50. reflect.ValueOf(f).Elem().Field(i).SetInt(value)
  51. }
  52. case string:
  53. value := field.String()
  54. if value != "" {
  55. reflect.ValueOf(f).Elem().Field(i).SetString(value)
  56. }
  57. }
  58. }
  59. }
  60. // Feeds is a list of feed
  61. type Feeds []*Feed