oap

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: MIT Imports: 7 Imported by: 0

README

Decode Apollo config to strcut field

Install via:

go install github.com/caiyunapp/oap

Usage like:

import "github.com/caiyunapp/oap"

type DemoConfig struct {
	Int      int           `apollo:"int"`
	Int8     int8          `apollo:"int8"`
	Int16    int16         `apollo:"int16"`
	Int32    int32         `apollo:"int32"`
	Int64    int64         `apollo:"int64"`
	Uint     uint          `apollo:"uint"`
	Uint8    uint8         `apollo:"uint8"`
	Uint16   uint16        `apollo:"uint16"`
	Uint32   uint32        `apollo:"uint32"`
	Uint64   uint64        `apollo:"uint64"`
	Duration time.Duration `apollo:"duration"`
}

func main(){
	// init your apollo client here
	// ...

	conf := &DemoConfig{}
	if err := oap.Decode(conf, client, make(map[string][]agollo.OpOption)); err != nil {
		panic(err)
	}
}

Support types:

  • String
  • Int
  • Bool
  • Float32
  • Float64
  • Struct from JSON or YAML

Documentation

Overview

Package oap is a library for loading Apollo configuration into a struct. It supports JSON/YAML/TOML format. You can also use custom unmarshal for struct type filed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(ptr interface{}, client agollo.Client, keyOpts map[string][]agollo.OpOption) error
Example
const (
	appid = "SampleApp"
	addr  = "http://81.68.181.139:8080"
	// http://81.68.181.139/app/access_key.html?#/appid=SampleApp
	secret = ""
)

if secret == "" {
	return
}

type FooConfig struct {
	Foo string `apollo:"abc"`
}

config := struct {
	FooConfig `apollo_namespace:"proper"`
}{}

client := agollo.NewClient(&agollo.Conf{
	AppID:           appid,
	MetaAddr:        addr,
	AccesskeySecret: secret,
})
if err := client.Start(); err != nil {
	panic(err)
}

if err := client.SubscribeToNamespaces("proper"); err != nil {
	panic(err)
}

fmt.Println(client.GetAllKeys(agollo.WithNamespace("application")))
fmt.Println(client.GetAllKeys(agollo.WithNamespace("proper")))

if err := oap.Decode(&config, client, make(map[string][]agollo.OpOption)); err != nil {
	panic(err)
}

fmt.Println(config.Foo)
Output:

func SetUnmarshalFunc

func SetUnmarshalFunc(name string, f UnmarshalFunc)

You can use custom unmarshal for struct type filed. Package oap provides built-in support for JSON/YAML/TOML.

Types

type UnmarshalFunc

type UnmarshalFunc func([]byte, interface{}) error

Jump to

Keyboard shortcuts

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