aconfig icon indicating copy to clipboard operation
aconfig copied to clipboard

Unable to parse slice of nested struct from JSON

Open nevian427 opened this issue 1 year ago • 1 comments

Hi,

Issue somewhat similar to #117 #120 Trying to load included JSON and got strange error 'load config: load files: no such field "Url" in struct'

package main

import (
	"errors"
	"fmt"
	"os"

	"github.com/cristalhq/aconfig"
	"github.com/davecgh/go-spew/spew"
)

// Application config
type config struct {
	APIBind   string       `json:"bind_api" required:"true"`      // address to bind HTTP API
	UDPBind   string       `json:"bind_udp" required:"true"`      // address to bind UDP event receiver
	Env       string       `json:"env" default:"prod"`            // execution environment [prod|local|dev]
	Store     string       `json:"store" default:"pg"`
	TLS       TLS          `json:"tls"`                           // mTLS config
	DB        DB           `json:"database" required:"true"`      // database component
	Validator Validator    `json:"validator" required:"true"`     // validator component
	Registrar []Registrar  `json:"registrar" required:"true"`     // registrar component(s)
	Notify    NotifyClient `json:"notify_client" required:"true"` // send notify events to
}

// DB configuration.
type DB struct {
	Host     string `json:"host" required:"true"`                 // db host
	User     string `json:"user" required:"true"`                 // db auth login
	Password string `json:"password" required:"true"`             // db auth passwd
	DB       string `json:"name" required:"true"`                 // db name
	Vtable   string `json:"validator_table" required:"true"`      // validator DB table
	Rtable   string `json:"registrar_table" default:"registrant"` // registrar DB table
	TLS      TLS    `json:"tls"`                                  // mTLS config
	Port     uint16 `json:"port" default:"5432"`                  // db port
}

type TLS struct {
	CACert string `json:"cacert"`      // CA cert
	Cert   string `json:"client_cert"` // mTLS cert
	Key    string `json:"client_key"`  // mTLS key
}

// validator registrar component configuration.
type Validator struct {
	URL         string `json:"url" required:"true"`          // management URL
	ContactHost string `json:"contact_host" required:"true"` // host in contact
}

// main registrar configuration.
type Registrar struct {
	URL string `json:"url" required:"true"` // management URL
}

type NotifyClient struct {
	URL string `json:"url" required:"true"` // URL send to
}

var ErrDoMigrate = errors.New("db migrate requested")
var CFG config

func main() {
	if err := Load(); err != nil {
		fmt.Println(err)
	}

	spew.Dump(CFG)
}

func Load() error {
	loader := aconfig.LoaderFor(&CFG, aconfig.Config{
		//		FlagPrefix: "trm",
		FileFlag: "config",
		Files:    []string{"config/trunkmanager.json"},
	})
	flags := loader.Flags() // <- IMPORTANT: use this to define your non-config flags
	migrate := flags.Bool("migrate", false, "do db migrations")
	if err := flags.Parse(os.Args[1:]); err != nil {
		return err
	}
	if err := loader.Load(); err != nil {
		return err
	}
	if *migrate {
		return ErrDoMigrate
	}
	return nil
}

trunkmanager.json

nevian427 avatar Dec 15 '23 16:12 nevian427

Hi, sorry for the problem and thanks for the detailed description. I will take a look shortly.

cristaloleg avatar Dec 15 '23 16:12 cristaloleg