Skip to content

Commit

Permalink
Adjust Manifest naming and visibility (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
msuozzo authored Feb 25, 2025
1 parent ad4e21c commit ca76131
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
32 changes: 16 additions & 16 deletions pkg/archive/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ import (

// Section represents a section in the manifest file
type Section struct {
// Attributes maintains the mapping of names to values for quick lookup
Attributes map[string]string
// Order maintains the original order of attributes
Order []string
// attributes maintains the mapping of names to values for quick lookup
attributes map[string]string
// Names of Attributes, by default maintaining their original order of appearance
Names []string
}

// NewSection creates a new section
func NewSection() *Section {
return &Section{
Attributes: make(map[string]string),
Order: make([]string, 0),
attributes: make(map[string]string),
Names: make([]string, 0),
}
}

// Set adds or updates an attribute while maintaining order
func (s *Section) Set(name, value string) {
if _, exists := s.Attributes[name]; !exists {
s.Order = append(s.Order, name)
if _, exists := s.attributes[name]; !exists {
s.Names = append(s.Names, name)
}
s.Attributes[name] = value
s.attributes[name] = value
}

// Get retrieves an attribute value
func (s *Section) Get(name string) (string, bool) {
v, ok := s.Attributes[name]
v, ok := s.attributes[name]
return v, ok
}

// Delete removes an attribute
func (s *Section) Delete(name string) {
if _, ok := s.Attributes[name]; !ok {
if _, ok := s.attributes[name]; !ok {
return
}
delete(s.Attributes, name)
for i, n := range s.Order {
delete(s.attributes, name)
for i, n := range s.Names {
if n == name {
s.Order = append(s.Order[:i], s.Order[i+1:]...)
s.Names = append(s.Names[:i], s.Names[i+1:]...)
break
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func ParseManifest(r io.Reader) (*Manifest, error) {
currentLine = line
if line == "" {
// Section separator
if currentSection != manifest.MainSection && len(currentSection.Order) > 0 {
if currentSection != manifest.MainSection && len(currentSection.Names) > 0 {
manifest.EntrySections = append(manifest.EntrySections, currentSection)
}
currentSection = NewSection()
Expand Down Expand Up @@ -184,7 +184,7 @@ func WriteManifest(w io.Writer, m *Manifest) error {

// writeSection writes a single section to a writer
func writeSection(w io.Writer, section *Section) error {
for _, name := range section.Order {
for _, name := range section.Names {
value, _ := section.Get(name)
if err := writeAttribute(w, name, value); err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions pkg/archive/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ func TestParseManifest(t *testing.T) {
return
}

if diff := cmp.Diff(got.MainSection.Attributes, tt.wantMain); diff != "" {
if diff := cmp.Diff(got.MainSection.attributes, tt.wantMain); diff != "" {
t.Errorf("Main section differs: (-got,+want)\n%s", diff)
}

var sections []map[string]string
for _, section := range got.EntrySections {
sections = append(sections, section.Attributes)
sections = append(sections, section.attributes)
}
if diff := cmp.Diff(sections, tt.wantEntry); diff != "" {
t.Errorf("Entry sections differ: (-got,+want)\n%s", diff)
Expand Down Expand Up @@ -203,14 +203,14 @@ func TestParseManifestOrder(t *testing.T) {
}

// Check main section order
if len(got.MainSection.Order) != len(tt.wantMainOrder) {
if len(got.MainSection.Names) != len(tt.wantMainOrder) {
t.Errorf("Main section order length = %d, want %d",
len(got.MainSection.Order), len(tt.wantMainOrder))
len(got.MainSection.Names), len(tt.wantMainOrder))
}
for i, name := range tt.wantMainOrder {
if got.MainSection.Order[i] != name {
if got.MainSection.Names[i] != name {
t.Errorf("Main section order[%d] = %s, want %s",
i, got.MainSection.Order[i], name)
i, got.MainSection.Names[i], name)
}
}
})
Expand Down

0 comments on commit ca76131

Please sign in to comment.