Skip to content

Commit

Permalink
Promote Artifact to interface (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp authored Aug 28, 2024
1 parent da73b21 commit 9b37b1e
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 232 deletions.
38 changes: 20 additions & 18 deletions artefactregistry.go → artifactregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,50 @@ import (
var _ ArtifactRegistry = (*InMemoryArtifactRegistry)(nil)

type InMemoryArtifactRegistry struct {
ArtifactMap map[string][]string
ArtifactMap map[string][]ArtifactID
mu sync.RWMutex // Mutex to protect concurrent access
}

func (r *InMemoryArtifactRegistry) Load() error { return nil }

func (r *InMemoryArtifactRegistry) Save() error { return nil }

func (r *InMemoryArtifactRegistry) AddArtifact(processorName string, artifactName string) {
func (r *InMemoryArtifactRegistry) AddArtifact(processorName string, artifactID ArtifactID) {
r.mu.Lock()
defer r.mu.Unlock()
if r.ArtifactMap == nil {
r.ArtifactMap = map[string][]string{}
r.ArtifactMap = map[string][]ArtifactID{}
}

if _, ok := r.ArtifactMap[processorName]; !ok {
r.ArtifactMap[processorName] = make([]string, 0)
r.ArtifactMap[processorName] = make([]ArtifactID, 0)
}
r.ArtifactMap[processorName] = append(r.ArtifactMap[processorName], artifactName)
r.ArtifactMap[processorName] = append(r.ArtifactMap[processorName], artifactID)
}

func (r *InMemoryArtifactRegistry) RemoveArtifact(processorName string, artifactName string) {
func (r *InMemoryArtifactRegistry) RemoveArtifact(processorName string, artifactID ArtifactID) {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.ArtifactMap[processorName]; !ok {
return
}
if r.ArtifactMap == nil {
r.ArtifactMap = map[string][]string{}
r.ArtifactMap = map[string][]ArtifactID{}
}

var artifacts []string
var artifacts []ArtifactID
for _, file := range r.ArtifactMap[processorName] {
if file != artifactName {
if file != artifactID {
artifacts = append(artifacts, file)
}
}

r.ArtifactMap[processorName] = artifacts
}

func (r *InMemoryArtifactRegistry) Artifacts(processorName string) []string {
func (r *InMemoryArtifactRegistry) Artifacts(processorName string) []ArtifactID {
if r.ArtifactMap == nil {
r.ArtifactMap = map[string][]string{}
r.ArtifactMap = map[string][]ArtifactID{}
}

values, ok := r.ArtifactMap[processorName]
Expand All @@ -67,6 +67,8 @@ func (r *InMemoryArtifactRegistry) Artifacts(processorName string) []string {

// JSONArtifactRegistry implementation of a ArtifactRegistry that is saved as a JSON file.
type JSONArtifactRegistry struct {
UseAbsolutePaths bool `json:"-"`

GeneratedAt time.Time `json:"generatedAt"`
ArtifactMap map[string]*JSONArtifactRegistryProcessor `json:"files"`
FilePath string `json:"-"`
Expand All @@ -76,7 +78,7 @@ type JSONArtifactRegistry struct {
}

type JSONArtifactRegistryProcessor struct {
Artifacts []string `json:"files"`
Artifacts []ArtifactID `json:"files"`
}

// NewJSONArtifactRegistry returns a new artifact file registry.
Expand Down Expand Up @@ -134,7 +136,7 @@ func (r *JSONArtifactRegistry) Save() error {
return nil
}

func (r *JSONArtifactRegistry) AddArtifact(processorName string, artifactName string) {
func (r *JSONArtifactRegistry) AddArtifact(processorName string, artifactID ArtifactID) {
r.mu.Lock()
defer r.mu.Unlock()

Expand All @@ -145,28 +147,28 @@ func (r *JSONArtifactRegistry) AddArtifact(processorName string, artifactName st
if _, ok := r.ArtifactMap[processorName]; !ok {
r.ArtifactMap[processorName] = &JSONArtifactRegistryProcessor{}
}
r.ArtifactMap[processorName].Artifacts = append(r.ArtifactMap[processorName].Artifacts, artifactName)
r.ArtifactMap[processorName].Artifacts = append(r.ArtifactMap[processorName].Artifacts, artifactID)
}

func (r *JSONArtifactRegistry) RemoveArtifact(processorName string, artifactName string) {
func (r *JSONArtifactRegistry) RemoveArtifact(processorName string, artifactID ArtifactID) {
r.mu.Lock()
defer r.mu.Unlock()

if _, ok := r.ArtifactMap[processorName]; !ok {
return
}

var files []string
var files []ArtifactID
for _, file := range r.ArtifactMap[processorName].Artifacts {
if file != artifactName {
if file != artifactID {
files = append(files, file)
}
}

r.ArtifactMap[processorName].Artifacts = files
}

func (r *JSONArtifactRegistry) Artifacts(processorName string) []string {
func (r *JSONArtifactRegistry) Artifacts(processorName string) []ArtifactID {
r.mu.RLock()
defer r.mu.RUnlock()

Expand Down
49 changes: 25 additions & 24 deletions artefactregistry_test.go → artifactregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestJSONArtifactRegistry_Load(t *testing.T) {
expectedError: nil,
expectedValue: &JSONArtifactRegistry{
GeneratedAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
ArtifactMap: map[string]*JSONArtifactRegistryProcessor{"processor1": {Artifacts: []string{"file1.txt"}}},
ArtifactMap: map[string]*JSONArtifactRegistryProcessor{"processor1": {Artifacts: []ArtifactID{"file1.txt"}}},
},
},
},
Expand Down Expand Up @@ -106,9 +106,10 @@ func TestJSONArtifactRegistry_Save(t *testing.T) {
{
name: "Successful Save",
given: &JSONArtifactRegistry{
UseAbsolutePaths: false,
ArtifactMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt"},
Artifacts: []ArtifactID{"file1.txt"},
},
},
},
Expand Down Expand Up @@ -168,32 +169,32 @@ func TestJSONArtifactRegistry_AddArtifact(t *testing.T) {
name string
initialMap map[string]*JSONArtifactRegistryProcessor
processorName string
artifactName string
artifactID ArtifactID
expectedMap map[string]*JSONArtifactRegistryProcessor
}{
{
name: "Add New Artifact",
initialMap: map[string]*JSONArtifactRegistryProcessor{},
processorName: "processor1",
artifactName: "file1.txt",
artifactID: "file1.txt",
expectedMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt"},
Artifacts: []ArtifactID{"file1.txt"},
},
},
},
{
name: "Add Artifact to Existing Processor",
initialMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file2.txt"},
Artifacts: []ArtifactID{"file2.txt"},
},
},
processorName: "processor1",
artifactName: "file1.txt",
artifactID: "file1.txt",
expectedMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file2.txt", "file1.txt"},
Artifacts: []ArtifactID{"file2.txt", "file1.txt"},
},
},
},
Expand All @@ -206,7 +207,7 @@ func TestJSONArtifactRegistry_AddArtifact(t *testing.T) {
}

// Act
registry.AddArtifact(tt.processorName, tt.artifactName)
registry.AddArtifact(tt.processorName, tt.artifactID)

// Assert
assert.Equal(t, tt.expectedMap, registry.ArtifactMap)
Expand All @@ -219,51 +220,51 @@ func TestJSONArtifactRegistry_RemoveArtifact(t *testing.T) {
name string
initialMap map[string]*JSONArtifactRegistryProcessor
processorName string
artifactName string
artifactID ArtifactID
expectedMap map[string]*JSONArtifactRegistryProcessor
}{
{
name: "Remove Existing Artifact",
initialMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt", "file2.txt"},
Artifacts: []ArtifactID{"file1.txt", "file2.txt"},
},
},
processorName: "processor1",
artifactName: "file1.txt",
artifactID: "file1.txt",
expectedMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file2.txt"},
Artifacts: []ArtifactID{"file2.txt"},
},
},
},
{
name: "Remove Non-Existing Artifact",
initialMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt"},
Artifacts: []ArtifactID{"file1.txt"},
},
},
processorName: "processor1",
artifactName: "file2.txt",
artifactID: "file2.txt",
expectedMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt"},
Artifacts: []ArtifactID{"file1.txt"},
},
},
},
{
name: "Remove From Non-Existing Processor",
initialMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt"},
Artifacts: []ArtifactID{"file1.txt"},
},
},
processorName: "processor2",
artifactName: "file1.txt",
artifactID: "file1.txt",
expectedMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt"},
Artifacts: []ArtifactID{"file1.txt"},
},
},
},
Expand All @@ -276,7 +277,7 @@ func TestJSONArtifactRegistry_RemoveArtifact(t *testing.T) {
}

// Act
registry.RemoveArtifact(tt.processorName, tt.artifactName)
registry.RemoveArtifact(tt.processorName, tt.artifactID)

// Assert
assert.Equal(t, tt.expectedMap, registry.ArtifactMap)
Expand All @@ -289,23 +290,23 @@ func TestJSONArtifactRegistry_Artifacts(t *testing.T) {
name string
initialMap map[string]*JSONArtifactRegistryProcessor
processorName string
expectedArtifacts []string
expectedArtifacts []ArtifactID
}{
{
name: "Get Artifacts for Existing Processor",
initialMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt", "file2.txt"},
Artifacts: []ArtifactID{"file1.txt", "file2.txt"},
},
},
processorName: "processor1",
expectedArtifacts: []string{"file1.txt", "file2.txt"},
expectedArtifacts: []ArtifactID{"file1.txt", "file2.txt"},
},
{
name: "Get Artifacts for Non-Existing Processor",
initialMap: map[string]*JSONArtifactRegistryProcessor{
"processor1": {
Artifacts: []string{"file1.txt"},
Artifacts: []ArtifactID{"file1.txt"},
},
},
processorName: "processor2",
Expand Down
15 changes: 7 additions & 8 deletions depresolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ import (
"strings"
)

const ResolvedDependencyContextArtifactName = "_resolved_dependencies"
const ResolvedDependenciesArtifactID = "_resolved_dependencies"

// ResolvedDependencies represents an ordered list of Specification that should be processed in that specific order to avoid
// unresolved types.
type ResolvedDependencies SpecificationGroup

func (r ResolvedDependencies) ID() ArtifactID {
return ResolvedDependenciesArtifactID
}

type DependencyProvider interface {
Supports(s Specification) bool
Provide(s Specification) []SpecificationName
Expand Down Expand Up @@ -68,16 +72,11 @@ func (p DependencyResolutionProcessor) Process(ctx ProcessingContext) ([]Artifac
}
ctx.Logger.Success("Dependencies resolved successfully.")

return []Artifact{
{
Name: ResolvedDependencyContextArtifactName,
Value: deps,
},
}, nil
return []Artifact{deps}, nil
}

func GetResolvedDependenciesFromContext(ctx ProcessingContext) ResolvedDependencies {
return GetContextArtifact[ResolvedDependencies](ctx, ResolvedDependencyContextArtifactName)
return GetContextArtifact[ResolvedDependencies](ctx, ResolvedDependenciesArtifactID)
}

type dependencySet map[SpecificationName]struct{}
Expand Down
Loading

0 comments on commit 9b37b1e

Please sign in to comment.