diff --git a/internal/module.go b/internal/module.go index da68483..8021466 100644 --- a/internal/module.go +++ b/internal/module.go @@ -1,6 +1,7 @@ package internal import ( + "encoding/json" "fmt" "sort" @@ -16,6 +17,22 @@ type Module struct { selectedVersion *Version } +func FromBytes(bts []byte) (*Module, error) { + module := &Module{} + err := json.Unmarshal(bts, module) + if err != nil { + return nil, err + } + module.Init() + return module, nil +} + +func (m *Module) Init() { + for _, version := range m.Versions { + version.Init() + } +} + // Prints the module name and its version selector // // If the module has a selected version, it is used instead of the default Latest() @@ -43,7 +60,7 @@ func (m *Module) HasVersion(version *semver.Version) bool { return false } -//VersionStrings serializes the versions to string +// VersionStrings serializes the versions to string func (m *Module) VersionStrings() []string { l := make([]string, len(m.Versions)) for i, v := range m.Versions { @@ -52,7 +69,7 @@ func (m *Module) VersionStrings() []string { return l } -//SelectVersion selects the highest version number which matches constraint +// SelectVersion selects the highest version number which matches constraint func (m *Module) SelectVersion(constraint *semver.Constraints) *Version { // descending sort of versions sort.SliceStable(m.Versions, func(i, j int) bool { return m.Versions[i].Version().GreaterThan(m.Versions[j].Version()) }) @@ -90,9 +107,3 @@ func (m *Module) Latest() *Version { sort.SliceStable(m.Versions, func(i, j int) bool { return m.Versions[i].Version().GreaterThan(m.Versions[j].Version()) }) return m.Versions[0] } - -func (m *Module) Init() { - for _, version := range m.Versions { - version.Init() - } -} diff --git a/internal/storage/s3/storage.go b/internal/storage/s3/storage.go index 5aa31d0..cabffc7 100644 --- a/internal/storage/s3/storage.go +++ b/internal/storage/s3/storage.go @@ -5,7 +5,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "os" "path" "strings" @@ -127,12 +127,14 @@ func (s *Storage) ModuleResources(ctx context.Context, module string, version st Recursive: true, }) { var bts []byte - if loadData { + // always load meta.json as basic support for piral framework + // to avoid a second network call to get meta.json content per module + if loadData || strings.HasSuffix(object.Key, "meta.json") { obj, err := s.minioClient.GetObject(ctx, s.bucket, object.Key, minio.GetObjectOptions{}) if err != nil { return nil, err } - bts, err = ioutil.ReadAll(obj) + bts, err = io.ReadAll(obj) if err != nil { errResp := minio.ToErrorResponse(err) if errResp.Code == "NoSuchKey" { @@ -188,8 +190,7 @@ func (s *Storage) loadModule(ctx context.Context, objectName string, loadData bo if err != nil { return nil, err } - var module = &internal.Module{} - bts, err := ioutil.ReadAll(manifestObj) + bts, err := io.ReadAll(manifestObj) if err != nil { errResp := minio.ToErrorResponse(err) if errResp.Code == "NoSuchKey" { @@ -197,12 +198,10 @@ func (s *Storage) loadModule(ctx context.Context, objectName string, loadData bo } return nil, err } - err = json.Unmarshal(bts, module) + module, err := internal.FromBytes(bts) if err != nil { return nil, err } - // FIXME: remove this hardcoded dependency MUST be called first after unmarshaling - module.Init() // load available resources for _, version := range module.Versions { version.Files, _ = s.ModuleResources(ctx, module.Name, version.Version().String(), loadData) diff --git a/rpc/get_url.go b/rpc/get_url.go index a9b1f41..7c3fb25 100644 --- a/rpc/get_url.go +++ b/rpc/get_url.go @@ -60,8 +60,8 @@ func (s *Server) GetUrl(ctx context.Context, c *connect.Request[moarpb.GetUrlReq selectedVersionString = version.Version().String() default: version = module.Latest() - selectedVersionString = "latest" s.logger.Debugf("Module (%s) version is not specified in query, defaulting to latest", request.ModuleName) + selectedVersionString = version.Value } var resources []*moarpb.VersionResource