Add helper methods for SubmitDialog structs (#9864)
* Add To/From JSON methods for SubmitDialog structs * Fix method ordering
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
71dba81e91
Коммит
21cbdb6de6
@@ -114,11 +114,6 @@ type SubmitDialogResponse struct {
|
|||||||
Errors map[string]string `json:"errors,omitempty"`
|
Errors map[string]string `json:"errors,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *PostActionIntegrationRequest) ToJson() []byte {
|
|
||||||
b, _ := json.Marshal(r)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenerateTriggerId(userId string, s crypto.Signer) (string, string, *AppError) {
|
func GenerateTriggerId(userId string, s crypto.Signer) (string, string, *AppError) {
|
||||||
clientTriggerId := NewId()
|
clientTriggerId := NewId()
|
||||||
triggerData := strings.Join([]string{clientTriggerId, userId, strconv.FormatInt(GetMillis(), 10)}, ":") + ":"
|
triggerData := strings.Join([]string{clientTriggerId, userId, strconv.FormatInt(GetMillis(), 10)}, ":") + ":"
|
||||||
@@ -198,6 +193,11 @@ func (r *OpenDialogRequest) DecodeAndVerifyTriggerId(s *ecdsa.PrivateKey) (strin
|
|||||||
return DecodeAndVerifyTriggerId(r.TriggerId, s)
|
return DecodeAndVerifyTriggerId(r.TriggerId, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *PostActionIntegrationRequest) ToJson() []byte {
|
||||||
|
b, _ := json.Marshal(r)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func PostActionIntegrationRequestFromJson(data io.Reader) *PostActionIntegrationRequest {
|
func PostActionIntegrationRequestFromJson(data io.Reader) *PostActionIntegrationRequest {
|
||||||
var o *PostActionIntegrationRequest
|
var o *PostActionIntegrationRequest
|
||||||
err := json.NewDecoder(data).Decode(&o)
|
err := json.NewDecoder(data).Decode(&o)
|
||||||
@@ -221,6 +221,34 @@ func PostActionIntegrationResponseFromJson(data io.Reader) *PostActionIntegratio
|
|||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SubmitDialogRequestFromJson(data io.Reader) *SubmitDialogRequest {
|
||||||
|
var o *SubmitDialogRequest
|
||||||
|
err := json.NewDecoder(data).Decode(&o)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SubmitDialogRequest) ToJson() []byte {
|
||||||
|
b, _ := json.Marshal(r)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func SubmitDialogResponseFromJson(data io.Reader) *SubmitDialogResponse {
|
||||||
|
var o *SubmitDialogResponse
|
||||||
|
err := json.NewDecoder(data).Decode(&o)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SubmitDialogResponse) ToJson() []byte {
|
||||||
|
b, _ := json.Marshal(r)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (o *Post) StripActionIntegrations() {
|
func (o *Post) StripActionIntegrations() {
|
||||||
attachments := o.Attachments()
|
attachments := o.Attachments()
|
||||||
if o.Props["attachments"] != nil {
|
if o.Props["attachments"] != nil {
|
||||||
|
|||||||
@@ -109,3 +109,52 @@ func TestPostActionIntegrationResponseFromJsonError(t *testing.T) {
|
|||||||
ro := PostActionIntegrationResponseFromJson(strings.NewReader(""))
|
ro := PostActionIntegrationResponseFromJson(strings.NewReader(""))
|
||||||
assert.Nil(t, ro)
|
assert.Nil(t, ro)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSubmitDialogRequestToJson(t *testing.T) {
|
||||||
|
t.Run("all fine", func(t *testing.T) {
|
||||||
|
request := SubmitDialogRequest{
|
||||||
|
URL: "http://example.org",
|
||||||
|
CallbackId: NewId(),
|
||||||
|
State: "some state",
|
||||||
|
UserId: NewId(),
|
||||||
|
ChannelId: NewId(),
|
||||||
|
TeamId: NewId(),
|
||||||
|
Submission: map[string]interface{}{
|
||||||
|
"text": "some text",
|
||||||
|
"float": 1.2,
|
||||||
|
"bool": true,
|
||||||
|
},
|
||||||
|
Cancelled: true,
|
||||||
|
}
|
||||||
|
jsonRequest := request.ToJson()
|
||||||
|
r := SubmitDialogRequestFromJson(bytes.NewReader(jsonRequest))
|
||||||
|
|
||||||
|
require.NotNil(t, r)
|
||||||
|
assert.Equal(t, request, *r)
|
||||||
|
})
|
||||||
|
t.Run("error", func(t *testing.T) {
|
||||||
|
r := SubmitDialogRequestFromJson(strings.NewReader(""))
|
||||||
|
assert.Nil(t, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubmitDialogResponseToJson(t *testing.T) {
|
||||||
|
t.Run("all fine", func(t *testing.T) {
|
||||||
|
request := SubmitDialogResponse{
|
||||||
|
Errors: map[string]string{
|
||||||
|
"text": "some text",
|
||||||
|
"float": "1.2",
|
||||||
|
"bool": "true",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
jsonRequest := request.ToJson()
|
||||||
|
r := SubmitDialogResponseFromJson(bytes.NewReader(jsonRequest))
|
||||||
|
|
||||||
|
require.NotNil(t, r)
|
||||||
|
assert.Equal(t, request, *r)
|
||||||
|
})
|
||||||
|
t.Run("error", func(t *testing.T) {
|
||||||
|
r := SubmitDialogResponseFromJson(strings.NewReader(""))
|
||||||
|
assert.Nil(t, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user