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

关于 yaml解析遇到的问题 #10

Closed
tank411 opened this issue Aug 22, 2019 · 1 comment
Closed

关于 yaml解析遇到的问题 #10

tank411 opened this issue Aug 22, 2019 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@tank411
Copy link

tank411 commented Aug 22, 2019

在解析yaml时候遇到了解析出错:
yam文件格式如下:

---
id: VEHICLE-RFID-0807112517
name: vehicle rfid pipeline
properties:
  kafka.bootstrap.servers: hdh98:9092,hdh197:9092,hdh199:9092
  kafka.zookeeper.connector: hdh98:2181,hdh197:2181,hdh199:2181
  hbase.zookeeper.quorum: hdh98:2181,hdh197:2181,hdh199:2181
  hbase.zookeeper.property.clientPort: 2181

查看了错误原因是因为解析时候对key的分割处理导致的。
read.go 文件中,144行处。

	if !strings.Contains(key, ".") {
		// c.addError(errNotFound)
		return
	}

	keys := strings.Split(key, ".")
	topK := keys[0]

修改后:

	if !strings.Contains(key, ":") {
		// c.addError(errNotFound)
		return
	}

	keys := strings.Split(key, ":")
	topK := keys[0]

在解析文件中希望可以修改,修改后查询使用的方式如下:
value2 := cfg.String("topics:0:properties:cleanup.policy")

@inhere inhere added the enhancement New feature or request label Aug 22, 2019
@inhere inhere self-assigned this Aug 22, 2019
@inhere
Copy link
Member

inhere commented Aug 24, 2019

@tank411 现在调整了下,支持自定义key的分隔符:

config/config_test.go

Lines 324 to 338 in 4388758

func TestDelimiter(t *testing.T) {
// options: Delimiter
is := assert.New(t)
c := New("test")
c.WithOptions(Delimiter(':'))
is.Equal(byte(':'), c.Options().Delimiter)
err := c.LoadData(map[string]interface{}{
"top0": 1,
"top1": map[string]int{"sub0": 2},
})
is.NoError(err)
// is.Equal(1, c.Int("top0"))
is.Equal(2, c.Int("top1:sub0"))
}

全局配置:

import github.com/gookit/config

config.WithOptions(config.Delimiter(':'))

// ...

config.String("properties:kafka.bootstrap.servers")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants