add a sample JsonType for process sql json

pull/351/head
Michael Li 2 years ago
parent 0aa6dae372
commit 81ae8c0596
No known key found for this signature in database

@ -8,47 +8,48 @@ import (
"github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/conf"
) )
// 数据库表名,统一使用 _<table name>_ 的形式命名, 比如tag表 => _tag_
var ( var (
_anouncement string _anouncement_ string
_anouncementContent string _anouncementContent_ string
_attachment string _attachment_ string
_captcha string _captcha_ string
_comment string _comment_ string
_commentContent string _commentContent_ string
_commentReply string _commentReply_ string
_contact string _contact_ string
_contactGroup string _contactGroup_ string
_message string _message_ string
_post string _post_ string
_postAttachmentBill string _postAttachmentBill_ string
_postCollection string _postCollection_ string
_postContent string _postContent_ string
_postStar string _postStar_ string
_tag string _tag_ string
_user string _user_ string
_walletRecharge string _walletRecharge_ string
_walletStatement string _walletStatement_ string
) )
func initTableName() { func initTableName() {
m := conf.DatabaseSetting.TableNames() m := conf.DatabaseSetting.TableNames()
_anouncement = m[conf.TableAnouncement] _anouncement_ = m[conf.TableAnouncement]
_anouncementContent = m[conf.TableAnouncementContent] _anouncementContent_ = m[conf.TableAnouncementContent]
_attachment = m[conf.TableAttachment] _attachment_ = m[conf.TableAttachment]
_captcha = m[conf.TableCaptcha] _captcha_ = m[conf.TableCaptcha]
_comment = m[conf.TableComment] _comment_ = m[conf.TableComment]
_commentContent = m[conf.TableCommentContent] _commentContent_ = m[conf.TableCommentContent]
_commentReply = m[conf.TableCommentReply] _commentReply_ = m[conf.TableCommentReply]
_contact = m[conf.TableContact] _contact_ = m[conf.TableContact]
_contactGroup = m[conf.TableContactGroup] _contactGroup_ = m[conf.TableContactGroup]
_message = m[conf.TableMessage] _message_ = m[conf.TableMessage]
_post = m[conf.TablePost] _post_ = m[conf.TablePost]
_postAttachmentBill = m[conf.TablePostAttachmentBill] _postAttachmentBill_ = m[conf.TablePostAttachmentBill]
_postCollection = m[conf.TablePostCollection] _postCollection_ = m[conf.TablePostCollection]
_postContent = m[conf.TablePostContent] _postContent_ = m[conf.TablePostContent]
_postStar = m[conf.TablePostStar] _postStar_ = m[conf.TablePostStar]
_tag = m[conf.TableTag] _tag_ = m[conf.TableTag]
_user = m[conf.TableUser] _user_ = m[conf.TableUser]
_walletRecharge = m[conf.TableWalletRecharge] _walletRecharge_ = m[conf.TableWalletRecharge]
_walletStatement = m[conf.TableWalletStatement] _walletStatement_ = m[conf.TableWalletStatement]
} }

@ -0,0 +1,64 @@
// Copyright 2023 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package types
import (
"database/sql"
"database/sql/driver"
stdjson "encoding/json"
"errors"
"fmt"
"github.com/rocboss/paopao-ce/pkg/json"
)
var (
_ stdjson.Marshaler = (*JsonType[any])(nil)
_ stdjson.Unmarshaler = (*JsonType[any])(nil)
_ driver.Valuer = (*JsonType[any])(nil)
_ sql.Scanner = (*JsonType[any])(nil)
)
type JsonType[T any] struct {
Data T
}
func (j *JsonType[T]) MarshalJSON() ([]byte, error) {
if j == nil {
return []byte(`null`), nil
}
return json.Marshal(j.Data)
}
func (j *JsonType[T]) UnmarshalJSON(data []byte) error {
if j == nil {
return errors.New("JSONText.UnmarshalJSON: on nil pointer")
}
return json.Unmarshal(data, &j.Data)
}
func (j *JsonType[T]) Value() (driver.Value, error) {
if j == nil {
return nil, nil
}
return j.MarshalJSON()
}
func (j *JsonType[T]) Scan(value any) error {
if value == nil {
return nil
}
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
return fmt.Errorf("JSONText.Scan: expected []byte or string, got %T (%q)", value, value)
}
return j.UnmarshalJSON(b)
}

@ -0,0 +1,60 @@
// Copyright 2023 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package types_test
import (
"encoding/json"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rocboss/paopao-ce/pkg/types"
)
var _ = Describe("Json", Ordered, func() {
type jsonCases []struct {
j types.JsonType[json.RawMessage]
b []byte
}
var samples jsonCases
BeforeAll(func() {
samples = jsonCases{
{
j: types.JsonType[json.RawMessage]{json.RawMessage(`null`)},
b: []byte(`null`),
},
{
j: types.JsonType[json.RawMessage]{json.RawMessage(`{}`)},
b: []byte(`{}`),
},
{
j: types.JsonType[json.RawMessage]{json.RawMessage(`[]`)},
b: []byte(`[]`),
},
{
j: types.JsonType[json.RawMessage]{json.RawMessage(`[{"b":true,"n":123},{"s":"foo","obj":{"f1":456,"f2":false}},[null]]`)},
b: []byte(`[{"b":true,"n":123},{"s":"foo","obj":{"f1":456,"f2":false}},[null]]`),
},
}
})
It("driver valuer ", func() {
for _, t := range samples {
v, err := t.j.Value()
Expect(err).To(BeNil())
Expect(v).To(Equal(t.b))
}
})
It("sql scan ", func() {
for _, t := range samples {
var jv types.JsonType[json.RawMessage]
err := jv.Scan(t.b)
Expect(err).To(BeNil())
Expect(jv.Data).To(Equal(t.j.Data))
}
})
})

@ -0,0 +1,17 @@
// Copyright 2023 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package types_test
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestTypes(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Types Suite")
}

@ -40,5 +40,4 @@ var _ = Describe("Md5", Ordered, func() {
Expect(util.EncodeMD5(t.value)).To(Equal(t.md5)) Expect(util.EncodeMD5(t.value)).To(Equal(t.md5))
} }
}) })
}) })

@ -1,3 +1,7 @@
// Copyright 2023 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package util_test package util_test
import ( import (

Loading…
Cancel
Save