GitRoot

craft your forge, build your project, grow your community freely
 1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: MIT
 4
 5package model
 6
 7import "github.com/tidwall/gjson"
 8
 9type FileActionType string
10
11const (
12	FileActionTypeAdd FileActionType = "add"
13	FileActionTypeMod FileActionType = "mod"
14	FileActionTypeDel FileActionType = "del"
15)
16
17type File struct {
18	Path        string         `json:"path"`
19	FileHash    string         `json:"fileHash"`
20	OldPath     string         `json:"oldPath"`     //only if action == FileActionTypeMod
21	OldFileHash string         `json:"oldFileHash"` //only if action == FileActionTypeMod
22	Action      FileActionType `json:"action"`
23}
24
25func FileFromString(file string) File {
26	j := gjson.Parse(file)
27	action := FileActionTypeAdd
28	switch j.Get("action").String() {
29	case "add":
30		action = FileActionTypeAdd
31	case "mod":
32		action = FileActionTypeMod
33	case "del":
34		action = FileActionTypeDel
35	}
36	return File{
37		Path:        j.Get("path").String(),
38		FileHash:    j.Get("fileHash").String(),
39		OldPath:     j.Get("oldPath").String(),
40		OldFileHash: j.Get("oldFileHash").String(),
41		Action:      action,
42	}
43}