github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/unstructured/properties.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package unstructured
    21  
    22  import (
    23  	"bytes"
    24  
    25  	"github.com/magiconair/properties"
    26  	"github.com/spf13/cast"
    27  
    28  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    29  )
    30  
    31  type propertiesConfig struct {
    32  	name       string
    33  	Properties *properties.Properties
    34  }
    35  
    36  const commentPrefix = "# "
    37  
    38  func init() {
    39  	CfgObjectRegistry().RegisterConfigCreator(appsv1alpha1.PropertiesPlus, func(name string) ConfigObject {
    40  		return &propertiesConfig{name: name}
    41  	})
    42  }
    43  
    44  func (p *propertiesConfig) Update(key string, value any) error {
    45  	_, _, err := p.Properties.Set(key, cast.ToString(value))
    46  	return err
    47  }
    48  
    49  func (p *propertiesConfig) RemoveKey(key string) error {
    50  	p.Properties.Delete(key)
    51  	return nil
    52  }
    53  
    54  func (p *propertiesConfig) Get(key string) interface{} {
    55  	val, ok := p.Properties.Get(key)
    56  	if ok {
    57  		return val
    58  	}
    59  	return nil
    60  }
    61  
    62  func (p *propertiesConfig) GetString(key string) (string, error) {
    63  	if val := p.Get(key); val != nil {
    64  		return val.(string), nil
    65  	}
    66  	return "", nil
    67  }
    68  
    69  func (p *propertiesConfig) GetAllParameters() map[string]interface{} {
    70  	r := make(map[string]interface{}, len(p.Properties.Keys()))
    71  	for _, key := range p.Properties.Keys() {
    72  		r[key] = p.Get(key)
    73  	}
    74  	return r
    75  }
    76  
    77  func (p *propertiesConfig) SubConfig(key string) ConfigObject {
    78  	return nil
    79  }
    80  
    81  func (p *propertiesConfig) Marshal() (string, error) {
    82  	if p.Properties.Len() == 0 {
    83  		return "", nil
    84  	}
    85  
    86  	var buf bytes.Buffer
    87  	_, err := p.Properties.WriteComment(&buf, commentPrefix, properties.UTF8)
    88  	if err != nil {
    89  		return "", err
    90  	}
    91  	return buf.String(), nil
    92  }
    93  
    94  func (p *propertiesConfig) Unmarshal(str string) (err error) {
    95  	l := &properties.Loader{
    96  		Encoding:         properties.UTF8,
    97  		DisableExpansion: true,
    98  	}
    99  	p.Properties, err = l.LoadBytes([]byte(str))
   100  	return err
   101  }