github.com/adacta-ru/mattermost-server/v6@v6.0.0/model/plugin_kvset_options.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"net/http"
     8  )
     9  
    10  // PluginKVSetOptions contains information on how to store a value in the plugin KV store.
    11  type PluginKVSetOptions struct {
    12  	Atomic          bool   // Only store the value if the current value matches the oldValue
    13  	OldValue        []byte // The value to compare with the current value. Only used when Atomic is true
    14  	ExpireInSeconds int64  // Set an expire counter
    15  }
    16  
    17  // IsValid returns nil if the chosen options are valid.
    18  func (opt *PluginKVSetOptions) IsValid() *AppError {
    19  	if !opt.Atomic && opt.OldValue != nil {
    20  		return NewAppError(
    21  			"PluginKVSetOptions.IsValid",
    22  			"model.plugin_kvset_options.is_valid.old_value.app_error",
    23  			nil,
    24  			"",
    25  			http.StatusBadRequest,
    26  		)
    27  	}
    28  
    29  	return nil
    30  }
    31  
    32  // NewPluginKeyValueFromOptions return a PluginKeyValue given a pluginID, a KV pair and options.
    33  func NewPluginKeyValueFromOptions(pluginId, key string, value []byte, opt PluginKVSetOptions) (*PluginKeyValue, *AppError) {
    34  	expireAt := int64(0)
    35  	if opt.ExpireInSeconds != 0 {
    36  		expireAt = GetMillis() + (opt.ExpireInSeconds * 1000)
    37  	}
    38  
    39  	kv := &PluginKeyValue{
    40  		PluginId: pluginId,
    41  		Key:      key,
    42  		Value:    value,
    43  		ExpireAt: expireAt,
    44  	}
    45  
    46  	return kv, nil
    47  }