go run . > output.png
-
-
Save dbrrt/780f8b2eda79be48e9b3e845d67d222f to your computer and use it in GitHub Desktop.
Generateur de QR Code tous anti covid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
_ "image/jpeg" | |
_ "image/png" | |
"io" | |
"log" | |
"os" | |
"text/template" | |
"time" | |
"github.com/skip2/go-qrcode" | |
) | |
func main() { | |
a := &Authorization{ | |
CreateDate: myTime{time.Now()}, | |
Name: "Doe", | |
FirstName: "John", | |
BirthDate: myTime{time.Now()}, | |
BirthCity: "Paris", | |
Address: "1 rue de Paris 75000 Paris", | |
AuthorizationDate: myTime{time.Now()}, | |
Purpose: "demarche", | |
} | |
err := a.Png(os.Stdout) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
type Authorization struct { | |
CreateDate myTime | |
Name string | |
FirstName string | |
BirthDate myTime | |
BirthCity string | |
Address string | |
AuthorizationDate myTime | |
/* Purpose is one of | |
"travail" | |
"sante" | |
"famille" | |
"handicap" | |
"judiciaire" | |
"missions" | |
"transit" | |
"animaux" | |
"courses" | |
"sport" | |
"rassemblement" | |
"demarche" | |
// source https://gitlab.inria.fr/stopcovid19/stopcovid-android/-/blob/master/stopcovid/src/main/assets/Attestations/form.json | |
*/ | |
Purpose string | |
} | |
const tmpl = `Cree le: {{ .CreateDate.Date }} a {{ .CreateDate.HourMinute }}; | |
Nom: {{ .Name }}; | |
Prenom: {{ .FirstName }}; | |
Naissance: {{ .BirthDate.Date }} a {{ .BirthCity }}; | |
Adresse: {{ .Address }}; | |
Sortie: {{ .AuthorizationDate.Date }} a {{ .AuthorizationDate.HourMinute }}; | |
Motifs: {{ .Purpose }}` | |
func (a *Authorization) String() string { | |
var b bytes.Buffer | |
t := template.Must(template.New("attestation").Parse(tmpl)) | |
err := t.Execute(&b, a) | |
if err != nil { | |
panic(err) | |
} | |
return b.String() | |
} | |
func (a *Authorization) Png(w io.Writer) error { | |
qrCode, err := qrcode.New(a.String(), qrcode.Medium) | |
if err != nil { | |
return err | |
} | |
return qrCode.Write(256, w) | |
} | |
type myTime struct { | |
time.Time | |
} | |
var months = [...]string{ | |
"janvier", | |
"fevrier", | |
"mars", | |
"avri", | |
"mai", | |
"juin", | |
"juillet", | |
"aout", | |
"septembre", | |
"octobre", | |
"novembre", | |
"decembre", | |
} | |
func (m myTime) Date() string { | |
return fmt.Sprintf("%v %v %v", m.Format("02"), months[m.Month()-1], m.Format("2006")) | |
} | |
func (m myTime) HourMinute() string { | |
return m.Format("15:04") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
_ "image/jpeg" | |
_ "image/png" | |
"testing" | |
"time" | |
) | |
func TestAuthorization_String(t *testing.T) { | |
const longForm = "Jan 2, 2006 at 3:04pm (MST)" | |
testTime, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)") | |
type fields struct { | |
CreateDate myTime | |
Name string | |
FirstName string | |
BirthDate myTime | |
BirthCity string | |
Address string | |
AuthorizationDate myTime | |
Purpose string | |
} | |
tests := []struct { | |
name string | |
fields fields | |
want string | |
}{ | |
{ | |
"simple", | |
fields{ | |
CreateDate: myTime{testTime}, | |
BirthDate: myTime{testTime}, | |
AuthorizationDate: myTime{testTime}, | |
}, | |
`Cree le: 03 fevrier 2013 a 19:54; | |
Nom: ; | |
Prenom: ; | |
Naissance: 03 fevrier 2013 a ; | |
Adresse: ; | |
Sortie: 03 fevrier 2013 a 19:54; | |
Motifs: `, | |
}, | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
a := &Authorization{ | |
CreateDate: tt.fields.CreateDate, | |
Name: tt.fields.Name, | |
FirstName: tt.fields.FirstName, | |
BirthDate: tt.fields.BirthDate, | |
BirthCity: tt.fields.BirthCity, | |
Address: tt.fields.Address, | |
AuthorizationDate: tt.fields.AuthorizationDate, | |
Purpose: tt.fields.Purpose, | |
} | |
got := a.String() | |
if got != tt.want { | |
t.Errorf("Authorization.String() = %v, want %v", got, tt.want) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment