首頁 > 後端開發 > Golang > 主體

編輯時在 YAML 檔案中保留單引號

PHPz
發布: 2024-02-12 12:03:08
轉載
391 人瀏覽過

编辑时在 YAML 文件中保留单引号

php小編香蕉介紹:在進行編輯時,保留單引號是一個重要的技巧。在YAML檔案中,使用單引號可以確保文字內容被原樣保留,不會受到解析器的解釋。這種方式可以避免特殊字元或特定格式的資料出現錯誤,確保文件內容的準確性和完整性。無論是處理設定檔還是編寫程式碼,保留單引號都是一個很好的習慣,能夠幫助我們更好地管理和維護程式碼。

問題內容

我想編輯 YAML 檔案中某些鍵的值,同時保持其餘鍵不變。我編寫了一個片段來為這些鍵插入一些值,但生成的新檔案不維護單引號 (')。如何避免這種情況?

我的程式碼:

<code>func updateVariables(nameValue, nameCluster string) error {
    
    yamlFile, err := os.ReadFile("path")
    if err != nil {
        return fmt.Errorf("Error reading YAML file: %v", err)
    }

    var config PipelineConfig

    err = yaml.Unmarshal(yamlFile, &config)
    if err != nil {
        return fmt.Errorf("Error parsing YAML file: %v", err)
    }

    for i := range config.Variables {
        switch config.Variables[i].Name {
        case "app_name":
            config.Variables[i].Value = nameValue
        case "cluster_name":
            config.Variables[i].Value = nameCluster

        }
    }

    modifiedYAML, err := yaml.Marshal(&config,)
    if err != nil {
        return fmt.Errorf("Error converting structure to YAML: %v", err)
    }

    err = os.WriteFile("path", modifiedYAML, 0644)
    if err != nil {
        return fmt.Errorf("Error writing modified YAML file: %v", err)
    }
    fmt.Println("File YAML modified.")
    return nil
}
</code>
登入後複製

我的結構:

<code>type PipelineConfig struct {
    Trigger   string `yaml:"trigger"`
    Variables []struct {
        Name  string `yaml:"name"`
        Value string `yaml:"value"`
    } `yaml:"variables"`
    
    Stages []struct {
        Template   string `yaml:"template"`
        Parameters struct {
            AppName       string `yaml:"app_name"`
            AppRepoBranch string `yaml:"app_repo_branch"`
            LocationEnv   string `yaml:"location_env"`
            ComponentName string `yaml:"component_name"`
            ClusterName   string `yaml:"cluster_name"`
        } `yaml:"parameters"`
    } `yaml:"stages"`
}
</code>
登入後複製

編輯前歸檔 yaml

trigger: none

variables:
  - name: app_name
    value: '<name>'
  - name: cluster_name
    value: '<cluster>'
  - name: component_name
    value: '<component>'
  - name: location_env
    value: 'dev'


stages:
  - template: 'tem'
    parameters:
      app_name: '${{ variables.app_name }}'
      app_repo_branch: 'dev'
      location_env: '${{ variables.location_env }}'
      component_name: '${{ variables.component_name }}'
      cluster_name: '${{ variables.cluster_name }}'
登入後複製

編輯後歸檔yaml

trigger: none
variables:
    - name: app_name
      value: test
    - name: cluster_name
      value: test
    - name: component_name
      value: <component>
    - name: location_env
      value: dev

stages:
    - template: tem
      parameters:
        app_name: ${{ variables.app_name }}
        app_repo_branch: develop
        location_env: ${{ variables.location_env }}
        component_name: ${{ variables.component_name }}
        cluster_name: ${{ variables.cluster_name }}
登入後複製

如您所見,單引號消失了。有什麼建議嗎?

解決方法

yaml.Unmarshal 函數將yaml 值解組到不帶元資料的自訂結構(stylekind 等)。 yaml.Marshal 函數正在處理自訂結構,將元資料值設為預設值。要存取元資料的字段,需要使用 yaml.Node$ $endc$$</a>。 </p> <p>在您的情況下 <code>Value 欄位具有 yaml.Style 等於 yaml.SingleQuotedStyle

要存取它(解組後不要遺失),請將 Value 欄位類型變更為 yaml.Node

Variables []struct {
    Name  string    `yaml:"name"`
    Value yaml.Node `yaml:"value"`
} `yaml:"variables"`
登入後複製
for i := range config.Variables {
    switch config.Variables[i].Name.Value {
    case "app_name":
        config.Variables[i].Value.Value = nameValue
    case "cluster_name":
        config.Variables[i].Value.Value = nameCluster
    }
}
登入後複製

遊樂場

以上是編輯時在 YAML 檔案中保留單引號的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!