diff --git a/cmd/hook/main.go b/cmd/hook/main.go index 02d5e3f..a5af35e 100644 --- a/cmd/hook/main.go +++ b/cmd/hook/main.go @@ -2,52 +2,16 @@ package main import ( "vultras.su/core/bond" - "vultras.su/core/bond/urlenc" + //"vultras.su/core/bond/urlenc" //"vultras.su/core/bond/methods" "vultras.su/core/bond/statuses" + "vultras.su/core/bond/jsons" "fmt" //"io" //"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 { Id int `json:"id"` @@ -59,20 +23,20 @@ type WebhookRequest struct { } type Leads struct { - Status JsonArrayMap[Status]`json:"status"` - Add JsonArrayMap[Status] `json:"add"` + Status jsons.ArrayMap[Status]`json:"status"` + Add jsons.ArrayMap[Status] `json:"add"` } type Status struct { - Id string `json:"id"` - StatusId string `json:"status_id"` - PipelineId string `json:"pipeline_id"` - OldStatusId string `json:"old_status_id"` - OldPipelineId string `json:"old_pipeline_id"` + Id jsons.Int `json:"id"` + StatusId jsons.Int`json:"status_id"` + PipelineId jsons.Int `json:"pipeline_id"` + OldStatusId jsons.Int `json:"old_status_id"` + OldPipelineId jsons.Int `json:"old_pipeline_id"` } type Account struct { - Id string `json:"id"` + Id jsons.Int `json:"id"` SubDomain string `json:"subdomain"` } @@ -120,7 +84,7 @@ Def( )) func main() { - requestString := "leads[status][0][id]=2050297&" + + /*requestString := "leads[status][0][id]=2050297&" + "leads[status][0][status_id]=35573056&" + "leads[status][0][pipeline_id]=3643927&" + "leads[status][0][old_status_id]=35572897&" + @@ -131,14 +95,14 @@ func main() { err := urlenc.Unmarshal([]byte(requestString), &reciever) if err != nil { panic(err) - } - fmt.Printf("%#v\n", reciever) - /*srv := bond.Server{ + }*/ + //fmt.Printf("%#v\n", reciever) + srv := bond.Server{ Addr: ":15080", Handler: root, } err := srv.ListenAndServe() if err != nil { panic(err) - }*/ + } } diff --git a/jsons/errors.go b/jsons/errors.go new file mode 100644 index 0000000..84f7273 --- /dev/null +++ b/jsons/errors.go @@ -0,0 +1,9 @@ +package jsons + +import ( + "errors" +) + +var ( + EmptyStringErr = errors.New("empty string") +) diff --git a/jsons/main.go b/jsons/main.go new file mode 100644 index 0000000..54f795c --- /dev/null +++ b/jsons/main.go @@ -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 +}