Skip to content
This repository was archived by the owner on Aug 30, 2020. It is now read-only.

Commit b6daffa

Browse files
author
Hongze Xia
committed
update entity.String
1 parent 0e15e10 commit b6daffa

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

models/entity.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"log"
9+
"strings"
810
"time"
911

1012
"cloud.google.com/go/datastore"
@@ -63,13 +65,21 @@ func (entity *Entity) Save(ctx context.Context, entTypName string, dsClient *dat
6365
return
6466
}
6567

66-
// String returns the JSON String representation
68+
// String returns a String representation
69+
// History has been skipped to save text space but the last one will be there
6770
func (entity *Entity) String() string {
68-
// marshal the entity as the message
69-
b, err := json.MarshalIndent(entity, "", " ")
70-
if err != nil {
71-
log.Print("failed to marshal entity", err)
72-
return ""
71+
var sb strings.Builder
72+
sb.WriteString(fmt.Sprintf("Product [%s] - K=%s\nURL: %s\nXPATH: %s\nHistory:\n",
73+
entity.Name, entity.K.Encode(), entity.URL, entity.XPATH))
74+
lastPrice := ""
75+
for i, data := range entity.History {
76+
if lastPrice != data.Price || i == len(entity.History)-1 {
77+
sb.WriteString(data.Timestamp.Format(time.RFC822))
78+
sb.WriteString("\t")
79+
sb.WriteString(data.Price)
80+
sb.WriteString("\n")
81+
lastPrice = data.Price
82+
}
7383
}
74-
return string(b)
84+
return sb.String()
7585
}

0 commit comments

Comments
 (0)