-
Notifications
You must be signed in to change notification settings - Fork 556
feat(bindings): add fromTemp metadata to localstorage #4280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,6 +57,7 @@ type LocalStorage struct { | |||||||||||||||||||||||||
| // Metadata defines the metadata. | ||||||||||||||||||||||||||
| type Metadata struct { | ||||||||||||||||||||||||||
| RootPath string `json:"rootPath"` | ||||||||||||||||||||||||||
| FromTemp bool `json:"fromTemp"` | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| type createResponse struct { | ||||||||||||||||||||||||||
|
|
@@ -91,6 +92,10 @@ func (ls *LocalStorage) parseMetadata(meta bindings.Metadata) (*Metadata, error) | |||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if m.FromTemp { | ||||||||||||||||||||||||||
| m.RootPath = filepath.Join(os.TempDir(), m.RootPath) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| m.RootPath = filepath.Join(os.TempDir(), m.RootPath) | |
| if strings.TrimSpace(m.RootPath) == "" { | |
| return nil, errors.New("rootPath must not be empty") | |
| } | |
| if filepath.IsAbs(m.RootPath) { | |
| return nil, errors.New("rootPath must not be absolute when fromTemp is enabled") | |
| } | |
| m.RootPath, err = securejoin.SecureJoin(os.TempDir(), m.RootPath) | |
| if err != nil { | |
| return nil, fmt.Errorf("invalid rootPath for temp directory: %w", err) | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,8 +37,21 @@ func TestParseMetadata(t *testing.T) { | |
| meta, err := localStorage.parseMetadata(m) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, path, meta.RootPath) | ||
| } | ||
|
|
||
| m.Properties = map[string]string{ | ||
| "rootPath": "myapp_data", | ||
| "fromTemp": "true", | ||
| } | ||
| metaTemp, err := localStorage.parseMetadata(m) | ||
| require.NoError(t, err) | ||
|
|
||
| realTempDir, err := filepath.EvalSymlinks(os.TempDir()) | ||
| require.NoError(t, err) | ||
| expectedTempPath := filepath.Join(realTempDir, "myapp_data") | ||
|
|
||
| assert.Equal(t, expectedTempPath, metaTemp.RootPath) | ||
| assert.True(t, metaTemp.FromTemp) | ||
| } | ||
|
Comment on lines
+41
to
+54
|
||
| func TestValidateRootPath(t *testing.T) { | ||
| // Get the current working directory | ||
| cwd, err := os.Getwd() | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -23,3 +23,8 @@ metadata: | |||||
| required: true | ||||||
| description: "The file name to write" | ||||||
| example: "data.txt" | ||||||
| - name: fromTemp | ||||||
| description: "If true, resolves rootPath from the system-wide temporary directory." | ||||||
|
||||||
| description: "If true, resolves rootPath from the system-wide temporary directory." | |
| description: "If true, resolves rootPath from the default temporary directory (os.TempDir())." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetComponentMetadatarelies onmetadata.GetMetadataInfoFromStructType, which usesmapstructuretags (and otherwise falls back to exported field names). Since these fields only havejsontags, the metadata map will expose keys likeRootPath/FromTemprather thanrootPath/fromTemp, diverging frommetadata.yamland other components. Consider addingmapstructure:"rootPath"andmapstructure:"fromTemp"tags (keeping the existingjsontags if needed) so metadata introspection and decoding are consistent.