Skip to content

Commit

Permalink
fix(yaml2md): add ParseFlatten() in Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
xbpk3t committed Dec 22, 2024
1 parent ca7f7b3 commit 7fc2422
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gh-merge/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func runMerge(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatalf("error reading file: %v", err)
}
// ft, _ := parser.NewParser[gh.ConfigRepos](fx).ParseFlatten()
// ft, _ := parser.NewParser[gh.ConfigRepo](fx).ParseFlatten()
ft := gh.NewConfigRepos(fx)
cr = append(cr, ft.WithTag(strings.TrimSuffix(file.Name(), ".yml"))...)
}
Expand Down
39 changes: 19 additions & 20 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"bytes"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -51,23 +52,21 @@ func (p *Parser[T]) ParseMulti() ([]T, error) {

// ParseFlatten 解析多文档 YAML 并展开结果
// 适用于每个文档都是 slice 且需要合并的情况
// func (p *Parser[T]) ParseFlatten() (T, error) {
// var result T
//
// d := yaml.NewDecoder(bytes.NewReader(p.data))
// for {
// // create new spec here
// spec := new(T)
// // pass a reference to spec reference
// if err := d.Decode(&spec); err != nil {
// // break the loop in case of EOF
// if errors.Is(err, io.EOF) {
// break
// }
// panic(err)
// }
//
// result = append(result, *spec...)
// }
// return result
// }
func (p *Parser[T]) ParseFlatten() ([]T, error) {
var results []T
decoder := yaml.NewDecoder(bytes.NewReader(p.data))

for {
var item []T
err := decoder.Decode(&item)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return nil, fmt.Errorf("解析配置失败: %w", err)
}
results = append(results, item...)
}

return results, nil
}
1 change: 1 addition & 0 deletions service/gh/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type XRenderer struct {
Config ConfigRepos
}

// TODO 1、实现该方法。2、移除x的--folder参数
func (x XRenderer) Render(data []byte) (string, error) {
return "", nil
}
2 changes: 1 addition & 1 deletion service/goods/goods.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *GoodsRenderer) Render(data []byte) (string, error) {

// ParseConfig 解析配置文件
func ParseConfig(data []byte) ([]Goods, error) {
return parser.NewParser[Goods](data).ParseMulti()
return parser.NewParser[Goods](data).ParseFlatten()
}

// RenderMarkdown 渲染为 Markdown 格式
Expand Down

0 comments on commit 7fc2422

Please sign in to comment.