Skip to content

refactor: extract duplicated annotation filepath helper #470

Description

@aftersnow

Description

The logic to extract filepath from layer annotations is duplicated in 3+ files:

  • pkg/backend/fetch.go
  • pkg/backend/fetch_by_d7y.go
  • pkg/backend/pull_by_d7y.go
var annoFilepath string
if desc.Annotations != nil {
    if desc.Annotations[modelspec.AnnotationFilepath] != "" {
        annoFilepath = desc.Annotations[modelspec.AnnotationFilepath]
    } else {
        annoFilepath = desc.Annotations[legacymodelspec.AnnotationFilepath]
    }
}

Suggested Fix

Extract into a shared helper in the backend package:

func getAnnotationFilepath(annotations map[string]string) string {
    if annotations == nil {
        return ""
    }
    if path := annotations[modelspec.AnnotationFilepath]; path != "" {
        return path
    }
    return annotations[legacymodelspec.AnnotationFilepath]
}

Context

Found during code review of #468. This is a pre-existing pattern, not introduced by that PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions