Skip to content

NHAS/tetcd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typesafe Etcd

This project combines code-generation and generics to hopefully provide a fairly usable typesafe harness for the KV store etcd. It generates both the path to your values, based on the struct you pass it when generating, and determines the types.

Example

For example, you have your configuration/database layout.

package config

type ServerConfig struct {
	Host string
	Port int
}

type TLSConfig struct {
	CertFile string
	KeyFile  string
}

type Config struct {
	Server ServerConfig
	TLS    TLSConfig

	Name   string
	Labels map[string]string
	Tags   []string `tetcd:"compress"`

	// should be skipped
	ignored      string
	SkippedField string `tetcd:"-"`
}

Then in another file in your project:

//go:generate go run github.com/NHAS/tetcd/cmd/tetcd-gen -type=github.com/NHAS/tetcd/cmd/test/config.Config -out=config_etcd.go 
// Code generated by https://github.com/NHAS/tetcd, DO NOT EDIT.
package main

import (
	codecs "github.com/NHAS/tetcd/codecs"
	paths "github.com/NHAS/tetcd/paths"
)

type autoTypeConfigServer struct{}

// Host() KV should contain type string
func (autoTypeConfigServer) Host() paths.Path[string] {
	return paths.NewPath("wagtest/Config/Server/Host", codecs.NewJsonCodec[string]())
}

// Port() KV should contain type int
func (autoTypeConfigServer) Port() paths.Path[int] {
	return paths.NewPath("wagtest/Config/Server/Port", codecs.NewJsonCodec[int]())
}

type autoTypeConfigTLS struct{}

// CertFile() KV should contain type string
func (autoTypeConfigTLS) CertFile() paths.Path[string] {
	return paths.NewPath("wagtest/Config/TLS/CertFile", codecs.NewJsonCodec[string]())
}

// KeyFile() KV should contain type string
func (autoTypeConfigTLS) KeyFile() paths.Path[string] {
	return paths.NewPath("wagtest/Config/TLS/KeyFile", codecs.NewJsonCodec[string]())
}

type autoTypeConfig struct {
	Server autoTypeConfigServer
	TLS    autoTypeConfigTLS
}

// Labels() is a map path with prefix wagtest/Config/Labels, value type string
func (autoTypeConfig) Labels() paths.MapPath[string] {
	return paths.NewMapPath("wagtest/Config/Labels", codecs.NewJsonCodec[string]())
}

// Name() KV should contain type string
func (autoTypeConfig) Name() paths.Path[string] {
	return paths.NewPath("wagtest/Config/Name", codecs.NewJsonCodec[string]())
}

// Tags() KV should contain type []string
func (autoTypeConfig) Tags() paths.Path[[]string] {
	return paths.NewPath("wagtest/Config/Tags", codecs.NewJsonCodec[[]string]())
}

var Config = autoTypeConfig{}

Then you can use it like this:

func UpdateLabels(labelName, contents string) error {
   	return Config.Labels().Key(labelName).Put(context.Background(), etcdclient, contents)
}

Configuration

generate

Flag Default Description
-prefix "" The global etcd key prefix
-type "" Fully qualified type to analyse, e.g. github.com/some/pkg.Config (required)
-pkg $GOPACKAGE Output package name
-out <type>_gen.go Output file path

Struct tags

The generator supports two tag options:

tetcd:"compress": Store value of slices/maps as value on a key rather than as keys under a prefix tetcd:"-": Ignore this struct field

Used

https://github.com/NHAS/wag

About

A typesafe harness for etcd

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages