How to use array or map in caddyfile

1. The problem I’m having:

after I write a plugin for caddy, I want to assign some variables in Caddyfile
the code just like this

repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
repl.Set("geoip2.subdivisions", record.Subdivisions)

the value record.Subdivisions is just like

[]struct {
		Locales    []string          `json:"locales"`
		Confidence uint16            `maxminddb:"confidence"`
		GeoNameID  uint              `maxminddb:"geoname_id"`
		IsoCode    string            `maxminddb:"iso_code"`
		Names      map[string]string `maxminddb:"names"`
	} `maxminddb:"subdivisions"`

so how can I. get the element in geoip2.subdivisions in caddyfile ?
just like

header geoip2_subdivisions_iso_code geoip2.subdivisions[0]["iso_code"]

but it is not works.

2. Error messages and/or full log output:


3. Caddy version:


4. How I installed and ran Caddy:

a. System environment:

Mac OS Ventura

You would need to set each item in the slice as a separate value in the replacer, or set up a replacer function with repl.Map which dynamically accesses an item in the slice by its index. Then you could use a placeholder like {geoip2.subdivisions.0.iso_code}. But you need to do that setup in Go code ahead of time.

I would set each variable like {geoip2.subdivisions.0.iso_code} and it works

Thank you for your help

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.