erldns_zone_codec behaviour (erldns v7.0.0-rc9)

View Source

Encoding and decoding of zone data in JSON format.

To write custom codecs, you need to implement the callbacks exposed by this module, and register the codec in the configuration.

Configuration

{erldns, [
    {zones, #{
        codecs => [sample_custom_zone_codec]
    }},
]}

Custom codecs

-module(sample_custom_zone_codec).
-behaviour(erldns_zone_codec).

-include_lib("dns_erlang/include/dns.hrl").
-include_lib("erldns/include/erldns.hrl").

-export([decode/1, encode/1]).

-define(DNS_TYPE_SAMPLE, 40000).

decode(#{~"name" := Name, ~"type" := ~"SAMPLE", ~"ttl" := Ttl, ~"data" := Data}) ->
    #dns_rr{
        name = Name,
        type = ?DNS_TYPE_SAMPLE,
        data = maps:get(~"dname", Data),
        ttl = Ttl
    };
decode(_) ->
    not_implemented.

encode(#dns_rr{name = Name, type = ?DNS_TYPE_SAMPLE, ttl = Ttl, data = Data}) ->
    #{
        ~"name" => Name,
        ~"type" => ~"SAMPLE",
        ~"ttl" => Ttl,
        ~"content" => erlang:iolist_to_binary(io_lib:format("~s", [Data]))
    };
encode(_) ->
    not_implemented.

Summary

Functions

Takes a JSON map and turns it into a zone.

Takes a zone and turns it into a map.

Get the list of registered zone parsers.

Register a custom parser module.

Register a list of custom parser modules.

Types

decoder()

-type decoder() :: fun((dynamic()) -> not_implemented | dns:rr()).

encoder()

-type encoder() :: fun((dns:rr()) -> not_implemented | json:encode_value()).

Callbacks

decode/1

-callback decode(json:encode_value()) -> not_implemented | dns:rr().

encode/1

-callback encode(dns:rr()) -> not_implemented | json:encode_value().

Functions

build_zone(Name, Version, Records, Keys)

-spec build_zone(dns:dname(), binary(), [dns:rr()], [erldns:keyset()]) -> erldns:zone().

decode(Zone)

-spec decode(json:decode_value()) -> erldns:zone().

Takes a JSON map and turns it into a zone.

encode(Zone)

-spec encode(erldns:zone()) -> json:encode_value().

Equivalent to encode(Zone, #{mode => zone_to_json}).

encode(Zone, Opts)

-spec encode(erldns:zone(), #{atom() => dynamic()}) -> json:encode_value().

Takes a zone and turns it into a map.

list_codecs()

-spec list_codecs() -> {[encoder()], [decoder()]}.

Get the list of registered zone parsers.

register_codec(Module)

-spec register_codec(module()) -> ok.

Register a custom parser module.

register_codecs(Modules)

-spec register_codecs([module()]) -> ok.

Register a list of custom parser modules.