Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrated 2024 diffs and trends grz #550

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions internal/snapshot/snapshot_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"net/url"
"os"
"path/filepath"
"strconv"

"github.com/r3labs/diff/v3"
)
Expand All @@ -35,7 +36,7 @@
return nil, fmt.Errorf("failed to diff snapshots: %w", err)
}

err = updateDiffSnap(changeLog, &diffSnap)

Check failure on line 39 in internal/snapshot/snapshot_diff.go

View workflow job for this annotation

GitHub Actions / Test Linting

ineffectual assignment to err (ineffassign)

out, err := json.Marshal(diffSnap)
if err != nil {
Expand All @@ -49,9 +50,9 @@
var bytes []byte
var err error

source := determineSource(path)

Check failure on line 53 in internal/snapshot/snapshot_diff.go

View workflow job for this annotation

GitHub Actions / Test Linting

ineffectual assignment to err (ineffassign)

switch source {

Check failure on line 55 in internal/snapshot/snapshot_diff.go

View workflow job for this annotation

GitHub Actions / Test Linting

ineffectual assignment to err (ineffassign)
case "url":
bytes, err = loadSnapshotFromUrl(path)
case "file":
Expand Down Expand Up @@ -84,7 +85,7 @@
}
}

return "snapshot"

Check failure on line 88 in internal/snapshot/snapshot_diff.go

View workflow job for this annotation

GitHub Actions / Test Linting

G107: Potential HTTP request made with variable url (gosec)
}

func loadSnapshotFromUrl(url string) ([]byte, error) {
Expand Down Expand Up @@ -113,5 +114,95 @@
}

func updateDiffSnap(changeLog diff.Changelog, diffSnap *map[string]interface{}) error {

for _, change := range changeLog {
var err error
topLevel := change.Path[0]
switch topLevel {
case "layout":
switch change.Type {
case "create":
err = addKeyValueAtPath(*diffSnap, change.Path, "__diff", "inserted")
case "delete":
err = addKeyValueAtPath(*diffSnap, change.Path, "__diff", "deleted")
// TODO: #snapshot we need to extract the deleted layout item and inject into the diff
case "update":
err = addKeyValueAtPath(*diffSnap, change.Path, "__diff", "updated")
default:
continue
}
if err != nil {
return fmt.Errorf("failed to update diff snapshot: %w", err)
}
case "panels":
switch change.Type {

Check failure on line 138 in internal/snapshot/snapshot_diff.go

View workflow job for this annotation

GitHub Actions / Test Linting

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
case "create":
err = addKeyValueAtPath(*diffSnap, change.Path, "__diff", "inserted")
case "delete":
fmt.Println("delete")
case "update":
updatePath := change.Path[:len(change.Path)-1]
updateKey := fmt.Sprintf("%s_diff", change.Path[len(change.Path)-1])
err = addKeyValueAtPath(*diffSnap, updatePath, "__diff", "updated")
err = addKeyValueAtPath(*diffSnap, updatePath, updateKey, change.From)
default:
continue
}
if err != nil {
return fmt.Errorf("failed to update diff snapshot: %w", err)
}
default:
continue
}
}
return nil
}

func addKeyValueAtPath(diffSnap map[string]interface{}, path []string, key string, value interface{}) error {
var current interface{} = diffSnap

// traverse path
for i, p := range path {
// end o path
if i == len(path)-1 {
switch typedCurrent := current.(type) {
case map[string]interface{}:
typedCurrent[key] = value
return nil
case []interface{}:
index, err := strconv.Atoi(p)
if err != nil || index < 0 || index >= len(typedCurrent) {
return fmt.Errorf("invalid index at path element '%s'", p)
}

if targetMap, ok := typedCurrent[index].(map[string]interface{}); ok {
targetMap[key] = value
return nil
}

return fmt.Errorf("expected map at index %d, got %T", index, typedCurrent[index])
default:
return fmt.Errorf("expected map or slice at path element '%s', got %T", p, current)
}
}

// traverse deeper
switch typedCurrent := current.(type) {
case map[string]interface{}:
if next, ok := typedCurrent[p]; ok {
current = next
} else {
return fmt.Errorf("path element '%s' not found", p)
}
case []interface{}:
index, err := strconv.Atoi(p)
if err != nil || index < 0 || index >= len(typedCurrent) {
return fmt.Errorf("invalid index '%s' at path element '%s'", p, p)
}
current = typedCurrent[index]
default:
return fmt.Errorf("expected map or slice at path element '%s', got %T", p, current)
}
}
return fmt.Errorf("failed to traverse path")
}
Loading