<commit_before>package helpers

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"

	"github.com/jinzhu/gorm"
)

var (
	ErrServiceOverloaded = errors.New("service overloaded")
	ErrUnknown           = errors.New("unknown error")
	ErrContentNotFound   = errors.New("content not found")
	ErrInternalError     = errors.New("internal error")
)

type Fallback func(string) error

var FallbackFn Fallback

type ErrorResponse struct {
	Description string `json:"description"`
	Error       string `json:"error"`
}

type BotChannelResponse struct {
	Data   BotChannelData `json:"data"`
	Status bool           `json:"status"`
}

type BotChannelData struct {
	ChannelId int64 `json:"channelId,string"`
}

//////////// PushRequest //////////////

type PushRequest struct {
	Body       string      `json:"body"`
	ChannelId  int64       `json:"channelId,string"`
	GroupName  string      `json:"groupName"`
	Token      string      `json:"token"`
	FallbackFn Fallback    `json:"-"`
	Payload    gorm.Hstore `json:"payload"`
	// EventType  map[string]*string `json:"eventType"`

}

// NewPushRequest creates a new PushRequest instance with Fallback function
func NewPushRequest(body string) *PushRequest {
	return &PushRequest{
		FallbackFn: FallbackFn,
		Body:       body,
	}
}

func (pr *PushRequest) Buffered() (io.Reader, error) {
	body, err := json.Marshal(pr)
	if err != nil {
		return nil, err
	}

	return bytes.NewReader(body), nil
}

// Fallback stores the Push request arguments in a queue, when a
// fallback function is attached to push request
func (pr *PushRequest) Fallback(token, rootPath string) error {
	if pr.FallbackFn == nil {
		return nil
	}

	fr := &FallbackRequest{}
	fr.Body = pr
	fr.RootPath = rootPath
	fr.Token = token

	body, err := json.Marshal(fr)
	if err != nil {
		return err
	}

	return pr.FallbackFn(string(body))
}

func FallbackHandler(message *string) error {
	if message == nil {
		return nil
	}

	fr := FallbackRequest{}
	err := json.Unmarshal([]byte(*message), &fr)
	if err != nil {
		return err
	}

	if err := fr.Push(); err != nil {
		return err
	}

	return nil
}

////////// FallbackRequest /////////////

type FallbackRequest struct {
	Token    string       `json:"token"`
	Body     *PushRequest `json:"body"`<commit_msg>helpers: SetPayload func is added<commit_after>package helpers

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"

	"github.com/jinzhu/gorm"
)

var (
	ErrServiceOverloaded = errors.New("service overloaded")
	ErrUnknown           = errors.New("unknown error")
	ErrContentNotFound   = errors.New("content not found")
	ErrInternalError     = errors.New("internal error")
)

type Fallback func(string) error

var FallbackFn Fallback

type ErrorResponse struct {
	Description string `json:"description"`
	Error       string `json:"error"`
}

type BotChannelResponse struct {
	Data   BotChannelData `json:"data"`
	Status bool           `json:"status"`
}

type BotChannelData struct {
	ChannelId int64 `json:"channelId,string"`
}

//////////// PushRequest //////////////

type PushRequest struct {
	Body       string      `json:"body"`
	ChannelId  int64       `json:"channelId,string"`
	GroupName  string      `json:"groupName"`
	Token      string      `json:"token"`
	FallbackFn Fallback    `json:"-"`
	Payload    gorm.Hstore `json:"payload"`
}

// NewPushRequest creates a new PushRequest instance with Fallback function
func NewPushRequest(body string) *PushRequest {
	return &PushRequest{
		FallbackFn: FallbackFn,
		Body:       body,
	}
}

func (pr *PushRequest) Buffered() (io.Reader, error) {
	body, err := json.Marshal(pr)
	if err != nil {
		return nil, err
	}

	return bytes.NewReader(body), nil
}

// Fallback stores the Push request arguments in a queue, when a
// fallback function is attached to push request
func (pr *PushRequest) Fallback(token, rootPath string) error {
	if pr.FallbackFn == nil {
		return nil
	}

	fr := &FallbackRequest{}
	fr.Body = pr
	fr.RootPath = rootPath
	fr.Token = token

	body, err := json.Marshal(fr)
	if err != nil {
		return err
	}

	return pr.FallbackFn(string(body))
}

func (pr *PushRequest) SetPayload(key string, value string) {
	if pr.Payload == nil {
		pr.Payload = gorm.Hstore{}
	}

	pr.Payload[key] = &value
}

func FallbackHandler(message *string) error {
	if message == nil {
		return nil
	}

	fr := FallbackRequest{}
	err := json.Unmarshal([]byte(*message), &fr)
	if err != nil {
		return err
	}

	if err := fr.Push(); err != nil {
		return err
	}

	return nil
}

////////// FallbackRequest /////////////

type FallbackRequest struct {
	Token    string       `json:"token"`
	Body     *PushRequest `json:"body"`
{'fraction_non_alphanumeric': 0.13775290572535515, 'fraction_numerical': 0.0017219113215669393, 'mean_word_length': 3.3027777777777776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7656294', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1491, 'n_words': 420}