亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Gomplate : 壞字符 U+0022 '-'

Gomplate : 壞字符 U+0022 '-'

Go
慕娘9325324 2022-07-11 17:30:49
我正在嘗試gomplate并遇到錯誤。對于上下文,我定義了一個模板文件 test.tmplt 和一個數據源文件 dev.yaml。test.tmplt 有以下內容:localAPIEndpoint:    advertiseAddress: {{ (datasource "k8s").api-advertise-ip }}而 dev.yaml 包含以下內容:api-advertise-ip: 192.168.0.1如果我嘗試使用 gomplate 填寫 test.tmplt 的內容,如下所示:gomplate -d k8s=./dev.yaml -f ./test.tmplt -o test.conf我收到以下錯誤:09:42:44 FTL  error="template: ./test.tmplt:2: bad character U+002D '-'"在我看來,它不喜歡模板文件中的“-”符號。任何解決方法?這是預期的行為嗎?編輯 1:感謝@icza 提供的答案,該答案適用于上述示例。但是,如果我將 yaml 文件修改為具有嵌套字段,它似乎會崩潰。例如dev.yaml:kubernetes:    api-advertise-ip: 192.168.0.0測試.tmplt:localAPIEndpoint:    advertiseAddress: {{ index (datasource "k8s") "kubernetes.api-advertise-ip" }}在這種情況下,輸出:gomplate -d k8s=./dev.yaml -f ./test.tmplt -o test.conf是 :localAPIEndpoint:    advertiseAddress: <no value>
查看完整描述

1 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

你的"k8s"數據源是一個 YAML 配置,你想訪問api-advertise-ip它的屬性。

由于api-advertise-ip包含破折號,因此您不能在模板中按原樣使用名稱,因為這是語法錯誤:模板引擎嘗試將其api用作屬性名稱,而后面的破折號是語法錯誤。

您必須將屬性名稱放在包含破折號的引號中:"api-advertise-ip"但是使用.選擇器也是無效的語法。

使用內置index函數通過以下鍵索引 YAML 數據源:

localAPIEndpoint:
    advertiseAddress: {{ index (datasource "k8s") "api-advertise-ip" }}

gomplatetext/template在引擎蓋下使用,請參閱Go Playground上的工作示例。

使用時index,如果您有多個嵌套級別,請將每個鍵作為附加參數提供給index.

例如:

localAPIEndpoint:
    advertiseAddress: {{ index (datasource "k8s") "kubernetes" "api-advertise-ip" }}

在Go Playground上試試這個。


查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 445 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號