Browse Source

fix: convert any to correct type while unmarshaling

Yeuoly 8 months ago
parent
commit
748993db5f

+ 15 - 3
cmd/commandline/bundle.go

@@ -139,24 +139,36 @@ func init() {
 	bundleCommand.AddCommand(bundlePackageCommand)
 	bundleCommand.AddCommand(bundleAnalyzeCommand)
 
-	bundleAppendDependencyCommand.Flags().StringP("bundle_path", "i", "", "path to the bundle file")
-	bundleAppendDependencyCommand.MarkFlagRequired("bundle_path")
-
 	bundleAppendGithubDependencyCommand.Flags().StringP("repo_pattern", "r", "", "github repo pattern")
+	bundleAppendGithubDependencyCommand.Flags().StringP("bundle_path", "i", "", "path to the bundle file")
 	bundleAppendGithubDependencyCommand.MarkFlagRequired("repo_pattern")
+	bundleAppendGithubDependencyCommand.MarkFlagRequired("bundle_path")
 
 	bundleAppendMarketplaceDependencyCommand.Flags().StringP("marketplace_pattern", "m", "", "marketplace pattern")
+	bundleAppendMarketplaceDependencyCommand.Flags().StringP("bundle_path", "i", "", "path to the bundle file")
 	bundleAppendMarketplaceDependencyCommand.MarkFlagRequired("marketplace_pattern")
+	bundleAppendMarketplaceDependencyCommand.MarkFlagRequired("bundle_path")
 
 	bundleAppendPackageDependencyCommand.Flags().StringP("package_path", "p", "", "path to the package")
+	bundleAppendPackageDependencyCommand.Flags().StringP("bundle_path", "i", "", "path to the bundle file")
 	bundleAppendPackageDependencyCommand.MarkFlagRequired("package_path")
+	bundleAppendPackageDependencyCommand.MarkFlagRequired("bundle_path")
 
 	bundleRemoveDependencyCommand.Flags().StringP("index", "i", "", "index of the dependency")
+	bundleRemoveDependencyCommand.Flags().StringP("bundle_path", "b", "", "path to the bundle file")
 	bundleRemoveDependencyCommand.MarkFlagRequired("index")
+	bundleRemoveDependencyCommand.MarkFlagRequired("bundle_path")
 
 	bundleBumpVersionCommand.Flags().StringP("target_version", "t", "", "target version")
+	bundleBumpVersionCommand.Flags().StringP("bundle_path", "b", "", "path to the bundle file")
 	bundleBumpVersionCommand.MarkFlagRequired("target_version")
+	bundleBumpVersionCommand.MarkFlagRequired("bundle_path")
 
 	bundlePackageCommand.Flags().StringP("output_path", "o", "", "output path")
+	bundlePackageCommand.Flags().StringP("bundle_path", "b", "", "path to the bundle file")
 	bundlePackageCommand.MarkFlagRequired("output_path")
+	bundlePackageCommand.MarkFlagRequired("bundle_path")
+
+	bundleAnalyzeCommand.Flags().StringP("bundle_path", "b", "", "path to the bundle file")
+	bundleAnalyzeCommand.MarkFlagRequired("bundle_path")
 }

+ 5 - 0
cmd/commandline/bundle/dep.go

@@ -140,6 +140,11 @@ func ListDependencies(bundlePath string) {
 		return
 	}
 
+	if len(dependencies) == 0 {
+		log.Info("No dependencies found")
+		return
+	}
+
 	for i, dependency := range dependencies {
 		log.Info("========== Dependency %d ==========", i)
 		if dependency.Type == bundle_entities.DEPENDENCY_TYPE_GITHUB {

+ 3 - 2
internal/core/bundle_packager/local.go

@@ -19,7 +19,7 @@ type LocalBundlePackager struct {
 
 func NewLocalBundlePackager(path string) (BundlePackager, error) {
 	// try read manifest file
-	manifestFile, err := os.Open(filepath.Join(path, "manifest.json"))
+	manifestFile, err := os.Open(filepath.Join(path, "manifest.yaml"))
 	if err != nil {
 		return nil, err
 	}
@@ -55,7 +55,8 @@ func NewLocalBundlePackager(path string) (BundlePackager, error) {
 			return err
 		}
 
-		assetName := strings.TrimPrefix(filePath, path+"/_assets/")
+		prefix := filepath.Join(path, "_assets")
+		assetName := strings.TrimPrefix(filePath, prefix)
 		packager.assets[assetName] = bytes.NewBuffer(assetBytes)
 
 		return nil

+ 1 - 1
internal/core/bundle_packager/zip.go

@@ -36,7 +36,7 @@ func NewZipBundlePackager(path string) (BundlePackager, error) {
 	}
 
 	// try read manifest file
-	manifestFile, err := zipReader.Open("manifest.json")
+	manifestFile, err := zipReader.Open("manifest.yaml")
 	if err != nil {
 		return nil, err
 	}

+ 58 - 4
internal/types/entities/bundle_entities/dependency.go

@@ -8,6 +8,7 @@ import (
 	"github.com/go-playground/validator/v10"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/entities/manifest_entities"
 	"github.com/langgenius/dify-plugin-daemon/internal/types/validators"
+	"gopkg.in/yaml.v3"
 )
 
 type DependencyType string
@@ -23,10 +24,56 @@ type Dependency struct {
 	Value any            `json:"value" yaml:"value" validate:"required"`
 }
 
+func (d *Dependency) UnmarshalYAML(node *yaml.Node) error {
+	// try convert Value to GithubDependency, MarketplaceDependency, PackageDependency
+	type alias struct {
+		Type  DependencyType `json:"type" yaml:"type" validate:"required,oneof=github marketplace package"`
+		Value yaml.Node      `json:"value" yaml:"value" validate:"required"`
+	}
+
+	var a alias
+	if err := node.Decode(&a); err != nil {
+		return err
+	}
+
+	d.Type = a.Type
+
+	// try convert Value to GithubDependency, MarketplaceDependency, PackageDependency
+	switch d.Type {
+	case DEPENDENCY_TYPE_GITHUB:
+		var value GithubDependency
+		if err := a.Value.Decode(&value); err != nil {
+			return err
+		}
+		d.Value = value
+	case DEPENDENCY_TYPE_MARKETPLACE:
+		var value MarketplaceDependency
+		if err := a.Value.Decode(&value); err != nil {
+			return err
+		}
+		d.Value = value
+	case DEPENDENCY_TYPE_PACKAGE:
+		var value PackageDependency
+		if err := a.Value.Decode(&value); err != nil {
+			return err
+		}
+		d.Value = value
+	}
+
+	return nil
+}
+
 type GithubRepoPattern string
 
 func (p GithubRepoPattern) Split() []string {
-	return strings.Split(string(p), "/")
+	split := strings.Split(string(p), ":")
+	// split again by "/"
+	splits := []string{}
+	for _, s := range split {
+		splits = append(splits, strings.Split(s, "/")...)
+	}
+
+	return splits
 }
 
 func (p GithubRepoPattern) Repo() string {
@@ -75,7 +122,14 @@ func NewMarketplacePattern(pattern string) (MarketplacePattern, error) {
 }
 
 func (p MarketplacePattern) Split() []string {
-	return strings.Split(string(p), "/")
+	split := strings.Split(string(p), ":")
+	// split again by "/"
+	splits := []string{}
+	for _, s := range split {
+		splits = append(splits, strings.Split(s, "/")...)
+	}
+
+	return splits
 }
 
 func (p MarketplacePattern) Organization() string {
@@ -114,7 +168,7 @@ var (
 		manifest_entities.VERSION_PATTERN,
 		manifest_entities.VERSION_PATTERN,
 	)
-	GITHUB_DEPENDENCY_PATTERN = fmt.Sprintf(`^[a-z0-9_-]{1,64}/[a-z0-9_-]{1,128}/%s/[^/]+$`, GITHUB_VERSION_PATTERN)
+	GITHUB_DEPENDENCY_PATTERN = fmt.Sprintf(`^[a-z0-9_-]{1,64}/[a-z0-9_-]{1,128}:%s/[^/]+$`, GITHUB_VERSION_PATTERN)
 
 	MARKETPLACE_VERSION_PATTERN = fmt.Sprintf(
 		`([~^]?%s|%s(\.%s){2}|%s-%s)`,
@@ -124,7 +178,7 @@ var (
 		manifest_entities.VERSION_PATTERN,
 		manifest_entities.VERSION_PATTERN,
 	)
-	MARKETPLACE_DEPENDENCY_PATTERN = fmt.Sprintf(`^[a-z0-9_-]{1,64}/[a-z0-9_-]{1,128}/%s$`, MARKETPLACE_VERSION_PATTERN)
+	MARKETPLACE_DEPENDENCY_PATTERN = fmt.Sprintf(`^[a-z0-9_-]{1,64}/[a-z0-9_-]{1,128}:%s$`, MARKETPLACE_VERSION_PATTERN)
 )
 
 var (

+ 24 - 24
internal/types/entities/bundle_entities/dependency_test.go

@@ -13,124 +13,124 @@ func TestGithubDependencyPatternRegex(t *testing.T) {
 		// Valid patterns
 		{
 			name:     "basic version pattern",
-			input:    "owner/repo/1.0.0/manifest.json",
+			input:    "owner/repo:1.0.0/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "version with patch",
-			input:    "owner/repo/1.0.1/manifest.yaml",
+			input:    "owner/repo:1.0.1/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "version with pre-release",
-			input:    "owner/repo/1.0.0-beta/manifest.json",
+			input:    "owner/repo:1.0.0-beta/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "version with x pattern",
-			input:    "owner/repo/1.x.x/manifest.json",
+			input:    "owner/repo:1.x.x/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "version with X pattern",
-			input:    "owner/repo/1.X.X/manifest.json",
+			input:    "owner/repo:1.X.X/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "version with mixed x pattern",
-			input:    "owner/repo/1.2.x/manifest.json",
+			input:    "owner/repo:1.2.x/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "version with tilde",
-			input:    "owner/repo/~1.0.0/manifest.json",
+			input:    "owner/repo:~1.0.0/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "version with caret",
-			input:    "owner/repo/^1.0.0/manifest.json",
+			input:    "owner/repo:^1.0.0/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "version range",
-			input:    "owner/repo/1.0.0-2.0.0/manifest.json",
+			input:    "owner/repo:1.0.0-2.0.0/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "complex owner and repo names",
-			input:    "complex-owner/complex-repo-name/1.0.0/manifest.json",
+			input:    "complex-owner/complex-repo-name:1.0.0/manifest.yaml",
 			expected: true,
 		},
 		{
 			name:     "underscore in names",
-			input:    "owner_name/repo_name/1.0.0/manifest.json",
+			input:    "owner_name/repo_name:1.0.0/manifest.yaml",
 			expected: true,
 		},
 
 		// Invalid patterns
 		{
 			name:     "four digit version",
-			input:    "owner/repo/1.0.0.1/manifest.json",
+			input:    "owner/repo:1.0.0.1/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "empty owner",
-			input:    "/repo/1.0.0/manifest.json",
+			input:    "/repo:1.0.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "empty repo",
-			input:    "owner//1.0.0/manifest.json",
+			input:    "owner//1.0.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "invalid version format",
-			input:    "owner/repo/1.0/manifest.json",
+			input:    "owner/repo:1.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "missing manifest file",
-			input:    "owner/repo/1.0.0/",
+			input:    "owner/repo:1.0.0/",
 			expected: false,
 		},
 		{
 			name:     "uppercase in owner",
-			input:    "Owner/repo/1.0.0/manifest.json",
+			input:    "Owner/repo:1.0.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "uppercase in repo",
-			input:    "owner/Repo/1.0.0/manifest.json",
+			input:    "owner/Repo:1.0.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "invalid characters in owner",
-			input:    "owner@/repo/1.0.0/manifest.json",
+			input:    "owner@/repo:1.0.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "invalid characters in repo",
-			input:    "owner/repo#/1.0.0/manifest.json",
+			input:    "owner/repo#:1.0.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "too long owner name",
-			input:    "ownerwithaverylongnamethatshouldnotbeallowedinthiscaseownerwithaverylongnamethatshouldnotbeallowedinthiscase/repo/1.0.0/manifest.json",
+			input:    "ownerwithaverylongnamethatshouldnotbeallowedinthiscaseownerwithaverylongnamethatshouldnotbeallowedinthiscase/repo:1.0.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "too long repo name",
-			input:    "owner/repowithavrepowithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirementsrepowithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirementserylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirements/1.0.0/manifest.json",
+			input:    "owner/repowithavrepowithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirementsrepowithaverylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirementserylongnamethatshouldnotbeallowedinthiscaseandshouldbeshorterthanspecifiedintherequirements:1.0.0/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "invalid version range format",
-			input:    "owner/repo/1.0.0-/manifest.json",
+			input:    "owner/repo:1.0.0-/manifest.yaml",
 			expected: false,
 		},
 		{
 			name:     "invalid pre-release format",
-			input:    "owner/repo/1.0.0-toolongprerelease/manifest.json",
+			input:    "owner/repo:1.0.0-toolongprerelease/manifest.yaml",
 			expected: false,
 		},
 	}