Skip to content

Commit

Permalink
all: Add custom JSON marshaller for sx1301 global config file
Browse files Browse the repository at this point in the history
  • Loading branch information
cvetkovski98 committed Nov 17, 2023
1 parent d03137b commit 86d11f8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/pfconfig/semtechudp/semtechudp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package semtechudp

import (
"encoding/json"

"go.thethings.network/lorawan-stack/v3/pkg/frequencyplans"
"go.thethings.network/lorawan-stack/v3/pkg/pfconfig/shared"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
Expand All @@ -28,6 +30,24 @@ type Config struct {
GatewayConf GatewayConf `json:"gateway_conf"`
}

// MarshalJSON implements json.Marshaler.
// Serializes the SX1301Conf field as an object if it contains a single element and as an array otherwise.
func (c Config) MarshalJSON() ([]byte, error) {
if len(c.SX1301Conf) == 1 {
return json.Marshal(
struct {
SX1301Conf *shared.SX1301Config `json:"SX1301_conf"`
GatewayConf GatewayConf `json:"gateway_conf"`
}{
SX1301Conf: c.SX1301Conf[0],
GatewayConf: c.GatewayConf,
},
)
}
type Alias Config
return json.Marshal((Alias)(c))
}

// GatewayConf contains the configuration for the gateway's server connection.
type GatewayConf struct {
GatewayID string `json:"gateway_ID,omitempty"`
Expand Down

0 comments on commit 86d11f8

Please sign in to comment.