Skip to content
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

LoadFromDir 返回类型 #173

Closed
zuozhehao opened this issue Oct 16, 2023 · 6 comments
Closed

LoadFromDir 返回类型 #173

zuozhehao opened this issue Oct 16, 2023 · 6 comments
Assignees
Labels
enhancement New feature or request resolved

Comments

@zuozhehao
Copy link

zuozhehao commented Oct 16, 2023

var data []*Data
config.Decode(&data)

返回的是map[string]*Data,不是[]*Data

@inhere
Copy link
Member

inhere commented Oct 17, 2023

能具体描述下吗?LoadFromDir 会用 文件名作为文件内容的 key (eg: path/to/dir/task.json => task)

@inhere inhere added the question Further information is requested label Oct 17, 2023
@zuozhehao
Copy link
Author

zuozhehao commented Oct 17, 2023

能具体描述下吗?LoadFromDir 会用 文件名作为文件内容的 key (eg: path/to/dir/task.json => task)

嗯,现在是返回map[string]any, 能否返回数组,多个文件组成一个大数组。

@inhere
Copy link
Member

inhere commented Oct 18, 2023

这样就完全没法取指定文件的配置了,跟平常的用法有点区别了。

@zuozhehao
Copy link
Author

这样就完全没法取指定文件的配置了,跟平常的用法有点区别了。

嗯,能否返回的时候做处理。

@inhere inhere added the enhancement New feature or request label Oct 19, 2023
@inhere
Copy link
Member

inhere commented Nov 7, 2023

config 底层的 data 始终需要一个 key 存数据


LoadFromDir(dirPath, format string) 我可以新增个参数 dataKey

LoadFromDir(dirPath, format string, dataKey string)
  • dataKey 默认 "" 使用文件名作为文件内容的key
  • dataKey="somekey" 设置key,搜集所有的文件内容塞到这个key 里组成slice. {somekey: [{file1 data}, {file2 data}, ...]}

decode 数据:

var data []*Data
config.MapStruct("somekey", &data)

inhere added a commit that referenced this issue Jan 12, 2024
- if set DataKey, will load all dir files data as Slice data
@inhere inhere added resolved and removed question Further information is requested labels Jan 12, 2024
@inhere
Copy link
Member

inhere commented Jan 13, 2024

可以升级到 https://github.com/gookit/config/releases/tag/v2.2.5 试试

@inhere inhere closed this as completed Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resolved
Projects
None yet
Development

No branches or pull requests

2 participants