elsyncfile

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SyncFile

type SyncFile struct {
	// contains filtered or unexported fields
}

A file object that will be synchronized with the target file. If you change the file directly, SyncFile will reload contents from the file. If you change the contents in SyncFile, changes will be applied to the file. Warning: All contents in the file will be loaded into memory, Please be careful to the size of the file.

func NewSyncFile

func NewSyncFile(filename string, interval time.Duration) (*SyncFile, error)

Create a SyncFile object.

func (*SyncFile) Apply

func (s *SyncFile) Apply() error

Apply changes to the file.

func (*SyncFile) Change

func (s *SyncFile) Change(new []byte) error

Change the contents

func (*SyncFile) Contents

func (s *SyncFile) Contents() []byte

Access to contents directly Important: If you changed the contents directly, You need run the Apply() function to apply changes.

func (*SyncFile) Control

func (s *SyncFile) Control() chan command

Access to control channel directly.

func (*SyncFile) Error

func (s *SyncFile) Error() error

Get latest error.

func (*SyncFile) Exit

func (s *SyncFile) Exit(wait bool)

Stop goroutines and close channels.

func (*SyncFile) Filename

func (s *SyncFile) Filename() string

Get filename

func (*SyncFile) GetString

func (s *SyncFile) GetString() string

Get contents as string.

func (*SyncFile) Interval

func (s *SyncFile) Interval() time.Duration

Get interval

func (*SyncFile) ModTime

func (s *SyncFile) ModTime() time.Time

Get modification time.

func (*SyncFile) Reload

func (s *SyncFile) Reload() error

Reload the file manually.

func (*SyncFile) RunCommand

func (s *SyncFile) RunCommand(cmd command)

Run commands.

type SyncJSON

type SyncJSON struct {
	SyncFile
	Json gjson.Result
}

A json dictionary file object that will be synchronized with the target file. If you change the file directly, SyncJSON will reload contents from the file. If you change the contents in SyncJSON, changes will be applied to the file. Warning: All contents in the file will be loaded into memory, Please be careful to the size of the file.

func NewSyncJSON

func NewSyncJSON(filename string, interval time.Duration) (*SyncJSON, error)

Create a SyncJSON object.

func (*SyncJSON) Array

func (s *SyncJSON) Array() []gjson.Result

Try return JSON as array

func (*SyncJSON) Get

func (s *SyncJSON) Get(key string) gjson.Result

Get value from dict.

func (*SyncJSON) GetArray

func (s *SyncJSON) GetArray(key string) []gjson.Result

Get value as array.

func (*SyncJSON) GetArrayOfInt

func (s *SyncJSON) GetArrayOfInt(key string) []int

Get value as a array of int.

func (*SyncJSON) GetArrayOfString

func (s *SyncJSON) GetArrayOfString(key string) []string

Get value as a array of string.

func (*SyncJSON) GetBool

func (s *SyncJSON) GetBool(key string) bool

Get value as bool.

func (*SyncJSON) GetFloat

func (s *SyncJSON) GetFloat(key string) float64

Get value as float.

func (*SyncJSON) GetInt

func (s *SyncJSON) GetInt(key string) int

Get value as int.

func (*SyncJSON) GetString

func (s *SyncJSON) GetString(key string) string

Get value as string.

func (*SyncJSON) Map

func (s *SyncJSON) Map() map[string]gjson.Result

Try return JSON as dict

type SyncStructController

type SyncStructController struct {
	// contains filtered or unexported fields
}

This controller can stop the sync-goroutines

func SyncStructWithJsonFile

func SyncStructWithJsonFile(structData interface{}, filename string, interval time.Duration) (*SyncStructController, error)

Create a goroutine to synchronize the data. If you want to stop the sync-goroutines, Use SyncStructController.Exit()

func (*SyncStructController) AddEventListener

func (s *SyncStructController) AddEventListener(name string, f func(string, gjson.Result, gjson.Result) error)

Add a event listener. When the value of this field in json file was changed, the listener function will be called. func(name string, old gjson.Result, new gjson.Result) error Important: name parameter is the field name in json file not in the struct. For example:

type Sample struct {
	 Name string `json:"name"`   <--- This json tag.
	 Age int `json:"age"`
}

If you want to add a event listener to `Name` field, the name parameter must be "name"

func (*SyncStructController) Apply

func (s *SyncStructController) Apply() error

Apply changes to file.

func (*SyncStructController) Error

func (s *SyncStructController) Error() error

Get latest error.

func (*SyncStructController) Exit

func (s *SyncStructController) Exit(wait bool)

Stop sync-goroutines.

func (*SyncStructController) Filename

func (s *SyncStructController) Filename() string

Get filename

func (*SyncStructController) Interval

func (s *SyncStructController) Interval() time.Duration

Get interval

func (*SyncStructController) ModTime

func (s *SyncStructController) ModTime() time.Time

Get modification time.

func (*SyncStructController) Reload

func (s *SyncStructController) Reload() error

Reload the file manually.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL