~samwhited/blogsync

ref: 5083f77d6a5a04a6c2e8f065c87351f48870505b blogsync/cmp.go -rw-r--r-- 1.6 KiB
5083f77dSam Whited .builds: verify DCO 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2019 The Blog Sync Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package main

import (
	"reflect"
	"time"

	"github.com/writeas/go-writeas/v2"
)

// eqPost returns whether two posts are equal for the purpose of updating them.
func eqPost(p1, p2 *writeas.Post) bool {
	switch {
	case p1 == nil && p2 == nil:
		return true
	case p1 == nil || p2 == nil:
		return false
	}

	switch {
	case p1.ID != p2.ID:
		return false
	case p1.Slug != p2.Slug:
		return false
	case p1.Font != p2.Font:
		return false
	case !reflect.DeepEqual(p1.Language, p2.Language):
		return false
	case !reflect.DeepEqual(p1.RTL, p2.RTL):
		return false
	case p1.Title != p2.Title:
		return false
	case p1.Content != p2.Content:
		return false
		// TODO: compare tags when we support them.
	}

	return true
}

func eqParams(p *writeas.Post, params *writeas.PostParams) bool {
	var created, updated time.Time
	if params.Updated != nil {
		updated = *params.Updated
	}
	if params.Created != nil {
		created = *params.Created
	}
	cmpPost := writeas.Post{
		ID:       params.ID,
		Slug:     params.Slug,
		Token:    params.Token,
		Font:     params.Font,
		Language: params.Language,
		RTL:      params.IsRTL,
		Created:  created,
		Updated:  updated,
		Title:    params.Title,
		Content:  params.Content,
		Views:    p.Views,
		// TODO: what is this?
		Listed: p.Listed,
		// TODO: implement tags
		Tags:      p.Tags,
		Images:    p.Images,
		OwnerName: p.OwnerName,
		// TODO: implement collection changing if ID is set on the post
		Collection: p.Collection,
	}
	return eqPost(&cmpPost, p)
}