Skip to content

Commit

Permalink
Avoid modifying the base package struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Jan 8, 2025
1 parent c1a9a6d commit 2b37951
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
1 change: 0 additions & 1 deletion packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ type BasePackage struct {
Categories []string `config:"categories,omitempty" json:"categories,omitempty" yaml:"categories,omitempty"`
SignaturePath string `config:"signature_path,omitempty" json:"signature_path,omitempty" yaml:"signature_path,omitempty"`
Discovery *Discovery `config:"discovery,omitempty" json:"discovery,omitempty" yaml:"discovery,omitempty"`
DataStreams []*DataStream `config:"data_streams,omitempty" json:"data_streams,omitempty" yaml:"data_streams,omitempty"`
}

// BasePolicyTemplate is used for the package policy templates in the /search endpoint
Expand Down
21 changes: 14 additions & 7 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func getSearchOutput(ctx context.Context, packageList packages.Packages) ([]byte
// Packages need to be sorted to be always outputted in the same order
sort.Sort(packageList)

var output []packages.BasePackage
var output []packageSummary
for _, p := range packageList {
data := getPackageSummaryOutput(p)
output = append(output, data)
Expand All @@ -187,20 +187,27 @@ func getSearchOutput(ctx context.Context, packageList packages.Packages) ([]byte
return util.MarshalJSONPretty(output)
}

func getPackageSummaryOutput(index *packages.Package) packages.BasePackage {
type packageSummary struct {
packages.BasePackage `json:",inline"`
DataStreams []*packages.DataStream `json:"data_streams,omitempty"`
}

func getPackageSummaryOutput(index *packages.Package) packageSummary {
summary := packageSummary{
BasePackage: index.BasePackage,
}
if len(index.DataStreams) == 0 {
return index.BasePackage
return summary
}

data := index.BasePackage
data.DataStreams = make([]*packages.DataStream, len(index.DataStreams))
summary.DataStreams = make([]*packages.DataStream, len(index.DataStreams))
for i, datastream := range index.DataStreams {
data.DataStreams[i] = &packages.DataStream{
summary.DataStreams[i] = &packages.DataStream{
Type: datastream.Type,
Dataset: datastream.Dataset,
Title: datastream.Title,
}
}

return data
return summary
}

0 comments on commit 2b37951

Please sign in to comment.