feat: added the jsons package for poorly made JSON.

This commit is contained in:
Andrey Parhomenko 2024-01-14 20:52:35 +03:00
parent f410b16797
commit 365b75dd9a
3 changed files with 113 additions and 52 deletions

View file

@ -2,52 +2,16 @@ package main
import ( import (
"vultras.su/core/bond" "vultras.su/core/bond"
"vultras.su/core/bond/urlenc" //"vultras.su/core/bond/urlenc"
//"vultras.su/core/bond/methods" //"vultras.su/core/bond/methods"
"vultras.su/core/bond/statuses" "vultras.su/core/bond/statuses"
"vultras.su/core/bond/jsons"
"fmt" "fmt"
//"io" //"io"
//"net/url" //"net/url"
"encoding/json" //"strconv"
"strconv"
) )
type JsonInt64 int64
func(ji *JsonInt) UnmarshalJSON(bts []byte) error {
k, err := strconv.ParseInt(string(bts), 10, 32)
if err != nil {
return err
}
}
type JsonArrayMap[V any] map[int] V
func (jam *JsonArrayMap[V]) UnmarshalJSON(bts []byte) error {
*jam = make(JsonArrayMap[V])
am := *jam
j := map[string] json.RawMessage{}
err := json.Unmarshal(bts, &j)
if err != nil {
return err
}
v := new(V)
for jk, jv := range j {
// Getting the key from string.
k, err := strconv.ParseInt(jk, 10, 32)
if err != nil {
return err
}
err = json.Unmarshal([]byte(jv), v)
if err != nil {
return err
}
am[int(k)] = *v
}
return nil
}
type GetNotesOptions struct { type GetNotesOptions struct {
Id int `json:"id"` Id int `json:"id"`
@ -59,20 +23,20 @@ type WebhookRequest struct {
} }
type Leads struct { type Leads struct {
Status JsonArrayMap[Status]`json:"status"` Status jsons.ArrayMap[Status]`json:"status"`
Add JsonArrayMap[Status] `json:"add"` Add jsons.ArrayMap[Status] `json:"add"`
} }
type Status struct { type Status struct {
Id string `json:"id"` Id jsons.Int `json:"id"`
StatusId string `json:"status_id"` StatusId jsons.Int`json:"status_id"`
PipelineId string `json:"pipeline_id"` PipelineId jsons.Int `json:"pipeline_id"`
OldStatusId string `json:"old_status_id"` OldStatusId jsons.Int `json:"old_status_id"`
OldPipelineId string `json:"old_pipeline_id"` OldPipelineId jsons.Int `json:"old_pipeline_id"`
} }
type Account struct { type Account struct {
Id string `json:"id"` Id jsons.Int `json:"id"`
SubDomain string `json:"subdomain"` SubDomain string `json:"subdomain"`
} }
@ -120,7 +84,7 @@ Def(
)) ))
func main() { func main() {
requestString := "leads[status][0][id]=2050297&" + /*requestString := "leads[status][0][id]=2050297&" +
"leads[status][0][status_id]=35573056&" + "leads[status][0][status_id]=35573056&" +
"leads[status][0][pipeline_id]=3643927&" + "leads[status][0][pipeline_id]=3643927&" +
"leads[status][0][old_status_id]=35572897&" + "leads[status][0][old_status_id]=35572897&" +
@ -131,14 +95,14 @@ func main() {
err := urlenc.Unmarshal([]byte(requestString), &reciever) err := urlenc.Unmarshal([]byte(requestString), &reciever)
if err != nil { if err != nil {
panic(err) panic(err)
} }*/
fmt.Printf("%#v\n", reciever) //fmt.Printf("%#v\n", reciever)
/*srv := bond.Server{ srv := bond.Server{
Addr: ":15080", Addr: ":15080",
Handler: root, Handler: root,
} }
err := srv.ListenAndServe() err := srv.ListenAndServe()
if err != nil { if err != nil {
panic(err) panic(err)
}*/ }
} }

9
jsons/errors.go Normal file
View file

@ -0,0 +1,9 @@
package jsons
import (
"errors"
)
var (
EmptyStringErr = errors.New("empty string")
)

88
jsons/main.go Normal file
View file

@ -0,0 +1,88 @@
package jsons
import (
"strconv"
"encoding/json"
)
// The package implements basic types
// to be used in structures that can be parsed
// from the poorly structured JSON. (Like ones that make IDs strings)
func parseInt(str string, n int) (int64, error) {
if str == "" {
return 0, EmptyStringErr
}
if str[0] == '"' {
str = str[1:]
}
if str == "" {
return 0, EmptyStringErr
}
if str[len(str)-1] == '"' {
str = str[:len(str)-1]
}
v, err := strconv.ParseInt(str, 10, n)
if err != nil {
return 0, err
}
return v, nil
}
type Int int
func(ji *Int) UnmarshalJSON(bts []byte) error {
v, err := parseInt(string(bts), 32)
if err != nil {
return err
}
*ji = Int(v)
return nil
}
type Int64 int64
func(ji *Int64) UnmarshalJSON(bts []byte) error {
v, err := parseInt(string(bts), 64)
if err != nil {
return err
}
*ji = Int64(v)
return nil
}
type ArrayMap[V any] map[int] V
func (jam *ArrayMap[V]) UnmarshalJSON(bts []byte) error {
*jam = make(ArrayMap[V])
am := *jam
j := map[string] json.RawMessage{}
err := json.Unmarshal(bts, &j)
if err != nil {
return err
}
v := new(V)
for jk, jv := range j {
// Getting the key from string.
k, err := strconv.ParseInt(jk, 10, 32)
if err != nil {
return err
}
err = json.Unmarshal([]byte(jv), v)
if err != nil {
return err
}
am[int(k)] = *v
}
return nil
}