93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
var albumsDataTest []Album
|
|
|
|
// StripNonCsvField strips all the fields which are not in the Csv
|
|
func StripNonCsvField(a Album) Album {
|
|
return Album{
|
|
ID: a.ID,
|
|
ISBN: a.ISBN,
|
|
Series: a.Series,
|
|
Title: a.Title,
|
|
Num: a.Num,
|
|
NumA: a.NumA,
|
|
State: a.State,
|
|
Author: a.Author,
|
|
Editor: a.Editor,
|
|
Collection: a.Collection,
|
|
SatID: a.SatID,
|
|
LegalDeposit: a.LegalDeposit,
|
|
PrintDate: a.PrintDate,
|
|
PurchaseDate: a.PurchaseDate,
|
|
}
|
|
}
|
|
|
|
func formatDate(w io.Writer, t time.Time, fakeError *bool) {
|
|
if t == time.Unix(0, 0) {
|
|
if *fakeError == true {
|
|
fmt.Fprintf(w, "00/0000;")
|
|
*fakeError = false
|
|
} else {
|
|
fmt.Fprintf(w, ";")
|
|
}
|
|
} else {
|
|
fmt.Fprintf(w, "%s;", t.Format("01/2006"))
|
|
}
|
|
}
|
|
|
|
var rxSatID = regexp.MustCompile(`\A([a-zA-Z]+)-([a-zA-Z0-9]+)\z`)
|
|
|
|
func GetCsvData() *bytes.Buffer {
|
|
res := &bytes.Buffer{}
|
|
|
|
fmt.Fprintf(res, "%s\n", "IdAlbum;ISBN;Serie;Num;NumA;Titre;Editeur;Collection;EO;DL;AI;Cote;Etat;DateAchat;PrixAchat;Note;Scenariste;Dessinateur;Wishlist;AVendre;Perso1;Perso2;Perso3;Perso4;Format;Suivi;Commentaire;Table")
|
|
|
|
fakeWrongTime := true
|
|
|
|
for _, a := range albumsDataTest {
|
|
fmt.Fprintf(res, "%d;%s;\"%s\";", a.ID, a.ISBN, a.Series)
|
|
|
|
if a.Num < 0 {
|
|
fmt.Fprintf(res, ";")
|
|
} else {
|
|
fmt.Fprintf(res, "%d;", a.Num)
|
|
}
|
|
|
|
fmt.Fprintf(res, "\"%s\";\"%s\";\"%s\";\"%s\";1;", a.NumA, a.Title, a.Editor, a.Collection)
|
|
formatDate(res, a.LegalDeposit, &fakeWrongTime)
|
|
formatDate(res, a.PrintDate, &fakeWrongTime)
|
|
|
|
m := rxSatID.FindStringSubmatch(a.SatID)
|
|
if m == nil {
|
|
panic(fmt.Sprintf("should not happen with %#v", a))
|
|
}
|
|
fmt.Fprintf(res, ";%d;%s;;;%s;;;;%s;%s;;;\"foo\";;;ALBUM\n", a.State, a.PurchaseDate.Format("02/01/2006"), a.Author, m[1], m[2])
|
|
}
|
|
return res
|
|
}
|
|
|
|
func init() {
|
|
albumsPath := filepath.Join("testdata", "albums.json")
|
|
f, err := os.Open(albumsPath)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Could not open '%s': %s", albumsPath, err))
|
|
}
|
|
defer CloseOrPanic(f, albumsPath)
|
|
|
|
dec := json.NewDecoder(f)
|
|
if err = dec.Decode(&albumsDataTest); err != nil {
|
|
panic(fmt.Sprintf("Could not parse '%s': %s", albumsPath, err))
|
|
}
|
|
}
|