<commit_before>package api

import (
	"encoding/hex"
	"encoding/json"
	"io"
	"regexp"

	"github.com/diyan/assimilator/interfaces"
	"github.com/diyan/assimilator/models"
	"github.com/labstack/echo"
	uuid "github.com/satori/go.uuid"
)

func storeGetView(c echo.Context) error {
	return renderNotImplemented(c)
}

var eventIDRegex = regexp.MustCompile("^[a-fA-F0-9]{32}$")

var validPlatforms = map[string]bool{
	"as3":        true,
	"c":          true,
	"cfml":       true,
	"cocoa":      true,
	"csharp":     true,
	"go":         true,
	"java":       true,
	"javascript": true,
	"node":       true,
	"objc":       true,
	"other":      true,
	"perl":       true,
	"php":        true,
	"python":     true,
	"ruby":       true,
	"elixir":     true,
	"haskell":    true,
	"groovy":     true,
}

type EventDetails struct {
	ProjectID           int
	EventID             string `in:"event_id"`
	models.EventDetails `in:",squash"`
	interfaces.EventInterfaces
}

func bindRequest(project models.Project, requestBody io.ReadCloser, event *EventDetails) error {
	event.ProjectID = project.ID

	rawEvent := map[string]interface{}{}
	if err := json.NewDecoder(requestBody).Decode(&rawEvent); err != nil {
		return err
	}
	// Ensure all keys are expected
	if err := models.DecodeRequest(rawEvent, &event); err != nil {
		return err
	}
	if !validPlatforms[event.Platform] {
		event.Platform = "other"
	}
	// TODO handle error
	event.EventInterfaces.DecodeRequest(rawEvent)
	return nil
}

func storePostView(c echo.Context) error {
	// TODO move GetProject to the package shared between api/endpoints and web/api
	//project := GetProject(c)
	project := models.Project{ID: 1}
	event := EventDetails{}
	if err := bindRequest(project, c.Request().Body, &event); err != nil {
		return err
	}
	return c.JSON(200, map[string]string{"id": event.EventID})
}

func newUUIDHexString() string {<commit_msg>Use mapstructure squash instead of explicit calls<commit_after>package api

import (
	"encoding/hex"
	"encoding/json"
	"io"
	"regexp"

	"github.com/diyan/assimilator/interfaces"
	"github.com/diyan/assimilator/models"
	"github.com/labstack/echo"
	uuid "github.com/satori/go.uuid"
)

func storeGetView(c echo.Context) error {
	return renderNotImplemented(c)
}

var eventIDRegex = regexp.MustCompile("^[a-fA-F0-9]{32}$")

var validPlatforms = map[string]bool{
	"as3":        true,
	"c":          true,
	"cfml":       true,
	"cocoa":      true,
	"csharp":     true,
	"go":         true,
	"java":       true,
	"javascript": true,
	"node":       true,
	"objc":       true,
	"other":      true,
	"perl":       true,
	"php":        true,
	"python":     true,
	"ruby":       true,
	"elixir":     true,
	"haskell":    true,
	"groovy":     true,
}

type EventDetails struct {
	ProjectID                  int
	EventID                    string `in:"event_id"`
	models.EventDetails        `in:",squash"`
	interfaces.EventInterfaces `in:",squash"`
}

func bindRequest(project models.Project, requestBody io.ReadCloser, event *EventDetails) error {
	event.ProjectID = project.ID

	eventMap := map[string]interface{}{}
	if err := json.NewDecoder(requestBody).Decode(&eventMap); err != nil {
		return err
	}
	// TODO Ensure all keys are expected
	eventMap = interfaces.ToAliasKeys(eventMap)
	if err := models.DecodeRequest(eventMap, &event); err != nil {
		return err
	}
	if !validPlatforms[event.Platform] {
		event.Platform = "other"
	}
	return nil
}

func storePostView(c echo.Context) error {
	// TODO move GetProject to the package shared between api/endpoints and web/api
	//project := GetProject(c)
	project := models.Project{ID: 1}
	event := EventDetails{}
	if err := bindRequest(project, c.Request().Body, &event); err != nil {
		return err
	}
	//pp.Print(event)
	return c.JSON(200, map[string]string{"id": event.EventID})
}

func newUUIDHexString() string {
{'fraction_non_alphanumeric': 0.14446185997910135, 'fraction_numerical': 0.004702194357366771, 'mean_word_length': 3.3660205245153936, 'pattern_counts': {'":': 38, '<': 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': '80601', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1346, 'n_words': 309}