Skip to content

Instantly share code, notes, and snippets.

@spiffcs
Created January 19, 2024 17:09
Show Gist options
  • Save spiffcs/72b641f04ac79d160ce079bcd3816382 to your computer and use it in GitHub Desktop.
Save spiffcs/72b641f04ac79d160ce079bcd3816382 to your computer and use it in GitHub Desktop.
Duplicate Relationships
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type Metadata struct {
Files []string
}
func main() {
filePath := "./test.json"
fileContent, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
var relationships []struct {
Parent string
Child string
Type string
Metadata Metadata
}
err = json.Unmarshal(fileContent, &relationships)
if err != nil {
fmt.Println("Error:", err)
return
}
// Create a map to track unique parent/child relationships
uniqueRelationships := make(map[string]struct{})
for _, rel := range relationships {
// Combine Parent and Child and Type to create a unique key
key := rel.Parent + "|" + rel.Child + "|" + rel.Type
// Check if the key already exists in the map
if _, exists := uniqueRelationships[key]; exists {
// Duplicate relationship found
fmt.Printf("Duplicate relationship: Parent=%s, Child=%s Type=%s\n", rel.Parent, rel.Child, rel.Type)
} else {
uniqueRelationships[key] = struct{}{}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment