-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility enable emit unpopulated and default values.
- Loading branch information
Michal Ližičiar
committed
Aug 12, 2024
1 parent
4a01b40
commit 01cae55
Showing
6 changed files
with
63 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package jsonencoder | ||
|
||
import ( | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/proto" | ||
|
||
jErrors "github.com/juju/errors" | ||
) | ||
|
||
type Config struct { | ||
EmitUnpopulated bool `mapstructure:"emitUnpopulated"` | ||
EmitDefaultValues bool `mapstructure:"emitDefaultValues"` | ||
} | ||
|
||
type Encoder struct { | ||
opts protojson.MarshalOptions | ||
} | ||
|
||
func NewOptions(cfg *Config) Encoder { | ||
return Encoder{ | ||
opts: protojson.MarshalOptions{EmitUnpopulated: cfg.EmitUnpopulated, EmitDefaultValues: cfg.EmitDefaultValues}, | ||
} | ||
} | ||
|
||
func (e Encoder) Format(m proto.Message) (string, error) { | ||
response, err := e.opts.Marshal(m) | ||
if err != nil { | ||
return "", jErrors.Trace(err) | ||
} | ||
|
||
return string(response), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters