NAME

Protobuf::WKT::Struct - Mixin for google.protobuf.Struct, Value, and ListValue

VERSION

version 0.04

SYNOPSIS

# In your .proto
import "google/protobuf/struct.proto";

message MyConfig {
  google.protobuf.Struct params = 1;
}

# In your Perl code
my $config = MyConfig->new;
my $struct = $config->params;

# Using from_perl to populate
$struct->from_perl({
    foo => "bar",
    baz => [1, 2.5, true],
    nested => { key => "val" }
});

# Using to_perl to convert back
my $hash_ref = $struct->to_perl();
# $hash_ref is now { foo => "bar", baz => [1, 2.5, 1], nested => { key => "val" } }

# Converting to JSON
my $json_string = $struct->to_json;

DESCRIPTION

This module provides helper methods for the generated classes corresponding to the google.protobuf.Struct, google.protobuf.Value, and google.protobuf.ListValue Well-Known Types. These methods are injected into the respective classes (Protobuf::WKT::Struct, Protobuf::WKT::Value, Protobuf::WKT::ListValue) which are used when google/protobuf/struct.proto is processed.

These types allow representing arbitrary JSON-like structures within protobuf messages.

METHODS for Protobuf::WKT::Struct

to_perl()

Converts the Struct message into a Perl HASH reference. Keys are strings, and values are recursively converted from Protobuf::WKT::Value objects to their Perl equivalents (scalars, ARRAY refs, HASH refs).

from_perl($hash_ref)

Populates the Struct message from a Perl HASH reference. The keys of the hash become the keys in the fields map. The values of the hash are recursively converted into Protobuf::WKT::Value messages.

Dies if $hash_ref is not a HASH reference.

Returns $self for chaining.

to_json()

Serializes the Struct message to a JSON string. This is equivalent to encoding the result of to_perl() using JSON::MaybeXS.

METHODS for Protobuf::WKT::Value

to_perl()

Converts the Value message to its native Perl representation based on which field in the kind oneof is set:

  • null_value: undef

  • number_value: Perl number

  • string_value: Perl string

  • bool_value: Perl boolean (1 or 0)

  • struct_value: HASH reference (recursively converted)

  • list_value: ARRAY reference (recursively converted)

from_perl($scalar)

Populates the Value message from a Perl scalar, ARRAY ref, or HASH ref.

  • undef: sets null_value

  • HASH ref: sets struct_value (recursively converted)

  • ARRAY ref: sets list_value (recursively converted)

  • Boolean (from JSON::MaybeXS::is_bool): sets bool_value

  • Looks like a number: sets number_value

  • Otherwise: sets string_value

Returns $self for chaining.

METHODS for Protobuf::WKT::ListValue

to_perl()

Converts the ListValue message into a Perl ARRAY reference. Each element is recursively converted using Protobuf::WKT::Value-to_perl()D>.

from_perl($array_ref)

Populates the ListValue message from a Perl ARRAY reference. Each element in the array is converted into a Protobuf::WKT::Value message using from_perl()D.

Dies if $array_ref is not an ARRAY reference.

Returns $self for chaining.

get_injected_methods()

Internal method used by Protobuf::ClassGenerator to list methods to inject into the class.

memory_profile()

Internal method for memory profiling.

SEE ALSO

Protobuf, Protobuf::Message, JSON::MaybeXS

AUTHOR

C.J. Collier <cjac@google.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Google LLC.

This is free software; you can redistribute it and/or modify it under the terms of the BSD 3-Clause License.