Documentation
¶
Index ¶
- Variables
- func CreateCacheDirPB(pb proto.Message, dirname string, filename string) error
- func ErrdKeyFalse(varname string) error
- func ErrdKeyNotSet(varname string) error
- func Exists(filename string) bool
- func ForceCreateCacheDirPB(pb proto.Message, dirname string, filename string) error
- func ForceCreatePB(pb proto.Message) error
- func GetFilename(pb proto.Message) (string, error)
- func GetProtoFile(pb proto.Message) (string, error)
- func GetProtobufName(pb proto.Message) (string, error)
- func GetString(pb proto.Message, varname string) (string, error)
- func GetThirdFieldName(pb proto.Message) (string, error)
- func HasChanged(name string) bool
- func IsDir(dirname string) bool
- func LoadCacheDir(pb proto.Message) error
- func LoadCacheDirByAppname(pb proto.Message, appname string) error
- func LoadConfigDir(pb proto.Message) error
- func LoadConfigPB(pb proto.Message, appname string, protoname string) (string, error)
- func LoadFromFilename(pb proto.Message, fullname string) error
- func LoadPanic(pb proto.Message) error
- func MakeCacheFilename(appname string, protoname string) string
- func MakeConfigFilename(appname string, protoname string) string
- func NewKeyNotSetError(varname string) error
- func Path(filename string) string
- func ReLoad(pb proto.Message) error
- func Save(pb proto.Message) error
- func SaveToFilename(pb proto.Message, fullname string) error
- func SaveWithHeader(pb proto.Message, header string) error
- func SetChanged(name string, b bool)
- func SetFilename(pb proto.Message, filename string) (bool, error)
- func VersionCheck(pb proto.Message) (string, string, error)
- func VersionCheckFile(pb proto.Message, filename string) (string, string, error)
- type KeyNotSetError
- type NotInitializedError
- type PatchError
Constants ¶
This section is empty.
Variables ¶
var ErrEmpty error = fmt.Errorf("config file was empty")
var ErrKeyNotSet = KeyNotSetError{}
A generic sentinel error for this category.
var ErrMarshal error = fmt.Errorf("protobuf parse error")
var ErrNotInitialized = NotInitializedError{}
Finally, declare a package-level variable of your new type. This is the "sentinel" error that users will compare against.
var ErrProtoFilename error = fmt.Errorf("proto does not have Filename")
var ErrProtoNoVarName error = errors.New("name not in .proto")
var ErrProtoVarNotString error = errors.New("name exists but is not a string")
var NotInitialized error = errors.New("your application config not initialized")
var VersionMismatch error = errors.New("your PB version does not match")
Functions ¶
func CreateCacheDirPB ¶ added in v0.0.30
checks the UUID and Version of the .pb file
func ErrdKeyFalse ¶ added in v0.0.24
func ErrdKeyNotSet ¶ added in v0.0.24
func ForceCreateCacheDirPB ¶ added in v0.0.33
checks the UUID and Version of the .pb file
func ForceCreatePB ¶ added in v0.0.37
checks the UUID and Version of the .pb file should re-create the file with the new .proto (unless the filename is bank)
func GetFilename ¶
this will try both "filename" and "Filename"
func GetProtoFile ¶ added in v0.0.30
GetProtoFile returns the name of the .proto file that defined the message.
func GetProtobufName ¶ added in v0.0.30
Because you followed autogenpb's advice (you did follow it right?) you now win the automation lottery.
for this .proto file, GetProtobufName(pb) returns "repos" Then, ConfigLoad(), ConfigSave(), CacheLoad() and CacheSave() all do exactly what is expected:
Automatically work with the files:
~/.config/<appname>/repos.pb
or
~/.cache/<appname/repos.pb
message Repos {
string uuid = 1;
string version = 2;
repeated Repo repos = 3;
func HasChanged ¶ added in v0.0.3
func LoadCacheDir ¶ added in v0.0.30
loads foo.proto from ~/.cache/<appname>/foo.pb
func LoadCacheDirByAppname ¶ added in v0.0.30
loads foo.proto from ~/.cache/<appname>/foo.pb
func LoadConfigDir ¶ added in v0.0.30
loads foo.proto from ~/.config/<appname>/foo.text
func LoadConfigPB ¶ added in v0.0.22
func LoadFromFilename ¶ added in v0.0.22
func MakeCacheFilename ¶ added in v0.0.30
~/.cache/appname/protoname.pb
func MakeConfigFilename ¶ added in v0.0.30
~/.config/appname/protoname.text
func NewKeyNotSetError ¶ added in v0.0.24
A helper function to create this error.
func ReLoad ¶ added in v0.0.30
uses the version to die. This is needed because loading binary protobuf files with rearranged messages is indeterminate
func SaveToFilename ¶ added in v0.0.22
writes the protobuf to disk (sets Filename if PB has 'Filename')
func SaveWithHeader ¶ added in v0.0.30
special case. adds a header to the TEXT output
func SetChanged ¶ added in v0.0.3
func SetFilename ¶
sets "Filename" if it exists in the protobuf
func VersionCheck ¶ added in v0.0.30
verify 'version' for .pb files application should die if they don't match returns (newVersion, oldVersion, error)
Types ¶
type KeyNotSetError ¶ added in v0.0.24
type KeyNotSetError struct {
KeyName string // It can hold data!
}
Let's make a more complex error for key-not-set issues.
func (KeyNotSetError) Error ¶ added in v0.0.24
func (e KeyNotSetError) Error() string
Make it satisfy the 'error' interface.
func (KeyNotSetError) Is ¶ added in v0.0.24
func (e KeyNotSetError) Is(target error) bool
(Your Goal) Define a custom Is() method. errors.Is(someError, ErrKeyNotSet) will call this method.
type NotInitializedError ¶ added in v0.0.24
type NotInitializedError struct{}
First, define a new, named type for your error. A struct is common.
func (NotInitializedError) Error ¶ added in v0.0.24
func (e NotInitializedError) Error() string
Now, attach the Error() method to your new type. This makes it satisfy the built-in 'error' interface.
func (NotInitializedError) String ¶ added in v0.0.24
func (e NotInitializedError) String() string
(Your Goal) Attach a custom String() method for pretty printing. This will be used by fmt.Println, for example.
type PatchError ¶ added in v0.0.17
type PatchError struct {
// Note: The `XXX_` fields are part of the older struct tags,
// but the reflection logic comes from the modern API.
// In a real generated file, you wouldn't see these tags.
UserName string `protobuf:"bytes,1,opt,name=user_name,json=userName,proto3" json:"user_name,omitempty"`
UserId int64 `protobuf:"varint,2,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
Email string `protobuf:"bytes,3,opt,name=email,proto3" json:"email,omitempty"`
LastLogin *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=last_login,json=lastLogin,proto3" json:"last_login,omitempty"`
}
--- Mock of a generated user.pb.go file ---
func (*PatchError) ProtoMessage ¶ added in v0.0.17
func (*PatchError) ProtoMessage()
func (*PatchError) ProtoReflect ¶ added in v0.0.17
func (x *PatchError) ProtoReflect() protoreflect.Message
In a real generated file, ProtoReflect() would be fully implemented. For this example, we don't need it as the marshallers will use reflection.
func (*PatchError) Reset ¶ added in v0.0.17
func (x *PatchError) Reset()
func (*PatchError) String ¶ added in v0.0.17
func (x *PatchError) String() string