Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to parse slice of nested struct from JSON #147

Open
nevian427 opened this issue Dec 15, 2023 · 1 comment
Open

Unable to parse slice of nested struct from JSON #147

nevian427 opened this issue Dec 15, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@nevian427
Copy link

nevian427 commented Dec 15, 2023

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

@cristaloleg cristaloleg added the bug Something isn't working label Dec 15, 2023
@cristaloleg
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants