Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: changed QR lib to a more flexible one #10

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
module github.com/gerifield/mnb-qr-go

go 1.14
go 1.23

require (
github.com/skip2/go-qrcode v0.0.0-20200519171959-a3b48390827e
github.com/stretchr/testify v1.7.0
github.com/yeqown/go-qrcode/v2 v2.2.4
github.com/yeqown/go-qrcode/writer/standard v1.2.4
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/fogleman/gg v1.3.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/yeqown/reedsolomon v1.0.0 // indirect
golang.org/x/image v0.10.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
50 changes: 47 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/skip2/go-qrcode v0.0.0-20200519171959-a3b48390827e h1:xVeSA6fTG0og2KsF+Jh9vzx8gYRtBfLmpXzp3L1eThY=
github.com/skip2/go-qrcode v0.0.0-20200519171959-a3b48390827e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yeqown/go-qrcode/v2 v2.2.4 h1:cXdYlrhzHzVAnJHiwr/T6lAUmS9MtEStjEZBjArrvnc=
github.com/yeqown/go-qrcode/v2 v2.2.4/go.mod h1:uHpt9CM0V1HeXLz+Wg5MN50/sI/fQhfkZlOM+cOTHxw=
github.com/yeqown/go-qrcode/writer/standard v1.2.4 h1:41e/aLr1AMVWlug6oUMkDg2r0+dv5ofB7UaTkekKZBc=
github.com/yeqown/go-qrcode/writer/standard v1.2.4/go.mod h1:H8nLSGYUWBpNyBPjDcJzAanMzYBBYMFtrU2lwoSRn+k=
github.com/yeqown/reedsolomon v1.0.0 h1:x1h/Ej/uJnNu8jaX7GLHBWmZKCAWjEJTetkqaabr4B0=
github.com/yeqown/reedsolomon v1.0.0/go.mod h1:P76zpcn2TCuL0ul1Fso373qHRc69LKwAw/Iy6g1WiiM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.10.0 h1:gXjUUtwtx5yOE0VKWq1CH4IJAClq4UGgUA3i+rpON9M=
golang.org/x/image v0.10.0/go.mod h1:jtrku+n79PfroUbvDdeUWMAI+heR786BofxrbiSF+J0=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34 changes: 26 additions & 8 deletions src/qr/qr.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package qr

import (
"bytes"
"errors"
"fmt"
"io"
"strings"
"time"

"github.com/skip2/go-qrcode"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard"
)

type Code struct {
Expand Down Expand Up @@ -79,7 +82,8 @@ func (a amount) String() string {
return fmt.Sprintf("%s%d", currency, a.total)
}

func (c Code) GeneratePNG(size int) ([]byte, error) {
// GeneratePNG .
func (c *Code) GeneratePNG(size int) ([]byte, error) {
if c.Valid.Expired() {
return nil, errors.New("negative validity period")
}
Expand All @@ -89,19 +93,28 @@ func (c Code) GeneratePNG(size int) ([]byte, error) {
return nil, errors.New("qr content is too large")
}

q, err := qrcode.New(qrContent, qrcode.Medium) // We should never hit this part
q, err := qrcode.NewWith(qrContent,
//qrcode.WithEncodingMode(qrcode.EncModeByte),
qrcode.WithErrorCorrectionLevel(qrcode.ErrorCorrectionMedium))
//qrcode.WithVersion(13)) -> With the standard's allowed 345 char size it will be bigger than 13, it will be 14
// -> This is expected since the \n will trigger binary encoding which could be 331 in 13 and 362 in 14 version (with M error correction)
if err != nil {
return nil, err
}

if q.VersionNumber > 13 { // This part should be unreachable
return nil, errors.New("generated image (version) is too high (content too big)")
if q.Dimension() > 73 { // 65 should be the max (ver 13), but 73 (ver 14) is the final size due to the binary encoding
return nil, fmt.Errorf("generated QR code (width size) %d is too high (content too big)", q.Dimension())
}
return q.PNG(size)

buf := bytes.NewBuffer(nil)
wr := standard.NewWithWriter(nopCloser{Writer: buf}, standard.WithQRWidth(uint8(size)))
err = q.Save(wr)

return buf.Bytes(), err
}

// String .
func (c Code) String() string {
func (c *Code) String() string {
var sb strings.Builder

sb.WriteString(c.Kind.String())
Expand Down Expand Up @@ -133,7 +146,6 @@ func (c Code) String() string {
sb.WriteString("\n")

if time.Time(c.Valid).IsZero() {
// TODO: Add test for this part
// Add a default time with one hour expire
sb.WriteString(date(time.Now().Add(time.Hour)).String())
} else {
Expand Down Expand Up @@ -366,3 +378,9 @@ func addRecipient(code *Code, bic, name, iban string) error {
code.IBAN = iban
return nil
}

type nopCloser struct {
io.Writer
}

func (nopCloser) Close() error { return nil }
23 changes: 14 additions & 9 deletions src/qr/qr_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package qr

import (
"fmt"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -161,21 +162,24 @@ func TestGeneratePNG(t *testing.T) {
assert.Equal(t, "negative validity period", err.Error())

_ = c.ValidUntil(time.Now().Add(time.Hour))
fmt.Println(c.String(), len(c.String()))
_, err = c.GeneratePNG(256)
assert.NoError(t, err)

// Fill all the fields and gen again and try to hit the version error
c = genFullCode(t)
assert.Len(t, c.String(), 483) // All the fields has this size, but the standard only allow 345 chars, so not all optional fields at the same time

_, err = c.GeneratePNG(64)
assert.Equal(t, "qr content is too large", err.Error())

// Test the max size (generated size with full content: 483, so remove some fields here
c.navCheckID = "" // -35
c.loyaltyID = "" // -35
c.credTranID = "" // -35
// Test the max size after removing some optional fields
c.shopID = "" // remaining -35
c.merchDevID = "" // remaining -35
c.invoiceID = "" // remaining -35
c.customerID = "12" // remaining -33

assert.Equal(t, qrContentMaxSize, len(c.String())) // This should be fine and check the qr after this
assert.Len(t, c.String(), qrContentMaxSize) // This should be fine and check the qr after this
_, err = c.GeneratePNG(64)
assert.NoError(t, err)
}
Expand Down Expand Up @@ -208,14 +212,14 @@ func TestFullCode(t *testing.T) {
}

func genFullCode(t *testing.T) *Code {
c, err := NewPaymentSend("abcdefgh", strings.Repeat("a", 70), "HU00123456789012345678901234")
c, err := NewPaymentSend(strings.Repeat("i", 11), strings.Repeat("a", 70), strings.Repeat("i", 28))
assert.NoError(t, err)

c.Version = version("111")
c.Charset = 2
assert.NoError(t, c.HUFAmount(999999999999))
c.Charset = 1
assert.NoError(t, c.HUFAmount(111111111111)) // HUF+12 char
assert.NoError(t, c.ValidUntil(time.Date(2120, 03, 30, 10, 11, 12, 0, time.FixedZone("testZone", 11))))
assert.NoError(t, c.Purpose("ACCT"))
assert.NoError(t, c.Purpose("ACCT")) // 4 char
assert.NoError(t, c.Message(strings.Repeat("b", 70)))
assert.NoError(t, c.ShopID(strings.Repeat("c", 35)))
assert.NoError(t, c.MerchDevID(strings.Repeat("d", 35)))
Expand All @@ -224,5 +228,6 @@ func genFullCode(t *testing.T) *Code {
assert.NoError(t, c.CredTranID(strings.Repeat("g", 35)))
assert.NoError(t, c.LoyaltyID(strings.Repeat("h", 35)))
assert.NoError(t, c.NavCheckID(strings.Repeat("i", 35)))

return c
}
Loading