WIP: add jsonconv by miekg · Pull Request #345 · psviderski/uncloud · GitHub
Skip to content

WIP: add jsonconv#345

Closed
miekg wants to merge 1 commit into
psviderski:mainfrom
miekg:miek/26/apr30do/normalizejson
Closed

WIP: add jsonconv#345
miekg wants to merge 1 commit into
psviderski:mainfrom
miekg:miek/26/apr30do/normalizejson

Conversation

@miekg

@miekg miekg commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

This package converts snake case (public_key) to camel case (PublicKey) for json output. We need this so all json generated by Uncloud has similar type of keys.

Specificaly generate json from grpc using different keys (snake case) and this isn't easily changed, hence to two pass to fix things.

This isn't a huge performance issue as this output is generated by running uc, it is not used for internal rpc or some such.

See discussion in #342 if we want this or not.

This package converts snake case (public_key) to camel case (PublicKey)
for json output. We need this so all json generated by Uncloud has
similar type of keys.

Specificaly generate json from grpc using different keys (snake case)
and this isn't easily changed, hence to two pass to fix things.

This isn't a huge performance issue as this output is generated by
running `uc`, it is not used for internal rpc or some such.

Signed-off-by: Miek Gieben <miek@miek.nl>
@psviderski

Copy link
Copy Markdown
Owner

@miekg

miekg commented Apr 30, 2026 via email

Copy link
Copy Markdown
Contributor Author

@miekg

miekg commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

I think I have an even better idea, even though this is hacky, but I think it will work.

We do own the protobuf definition and it is just Go code, so why don't we rewrite the generated protobuf files and just update the - in our eyes - correct json tags..., I don't think they are used for something else

that means reworking this PR in a go generate that becomes part of the build process.

and look it already exists: https://github.com/fatih/gomodifytags
(shold prolly need a little more work, but ....)

@miekg

miekg commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author
gomodifytags -file machine.pb.go -all -remove-tags json | gomodifytags -file /dev/stdin -all -add-tags json -add-options json=omitempty -transform pascalcase | less

til this is actually called pascalcase.

type MachineInfo struct {
        state         protoimpl.MessageState  `json:"State,omitempty"`
        sizeCache     protoimpl.SizeCache     `json:"SizeCache,omitempty"`
        unknownFields protoimpl.UnknownFields `json:"UnknownFields,omitempty"`

        Id       string         `protobuf:"bytes,1,opt,name=id,proto3" json:"Id,omitempty"`
        Name     string         `protobuf:"bytes,2,opt,name=name,proto3" json:"Name,omitempty"`
        Network  *NetworkConfig `protobuf:"bytes,3,opt,name=network,proto3" json:"Network,omitempty"`
        PublicIp *IP            `protobuf:"bytes,4,opt,name=public_ip,json=publicIp,proto3" json:"PublicIp,omitempty"`
}

is exactly what we want, and saves (a quite the hassle) redefining everything

@psviderski

psviderski commented May 1, 2026

Copy link
Copy Markdown
Owner

I don't think they are used for something else

The serialised JSONs are stored in Corrosion in the machines table. So this will be a breaking change we need to account.

It fixes JSON keys but does nothing for Go ergonomics. The caller still receives *pb.IP, *pb.IPPort, *pb.MachineMember, custom Duration and Timestamp and have to call ToAddr(), ToPrefix(), handle nils and errors everywhere. In some places I even started ignoring the err returned by these conversion methods when I was sure the data was validated before. It's just so much boilerplate.

Proto types can't carry domain methods (Validate, Clone, Equal) and you still can't automatically attach methods like Equals on regenerated types without risk anyway.

I think we still want to maintain Go-native domain data types at least for commonly used resources such as Machine.

They also not always map 1:1 to the RPC wire data types. For example, we have Service that doesn't exist at the RPC level and is comprised of individual container calls fully on the client. This might change in the future and we can change the RPC wire level potentially without even affecting the user-facing Service.

Our client methods are also not always 1:1 mapping to the RPC methods because we often need to do the low level proxy/broadcast routine. So far we've been pretty successful at hiding these complexities from the user of the client but still provide a clear interface, for example, to specify if a particular action needs to run on a specific machine using a simple argument or a field in an options struct.

Now, it would be great to draw a clear line what we should mirror/duplicate and what not.

Mirroring all the proto types feels like it defeats the whole purpose of using proto in the first place. I don't know to be honest and I'm fine to not overthink it.

I would probably not bother to recreate type like UpdateMachineRequest, JoinClusterRequest, AddMachineRequest which are used only in one method and which are used very rarely. The only problem is inconsistency. But again we have much bigger fish to fry at the moment than to try to perfect these types.
I'm sure we will work out some intuition and maybe better ideas after working more with those.

@miekg

miekg commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

The serialised JSONs are stored in Corrosion in the machines table. So this will be a breaking change we need to account.

ah, then this route is a no-go

The serialised JSONs are stored in Corrosion in the machines table. So this will be a breaking change we need to account.

Very true. Hence the WIP status here, and good to have some docs on subject. Also saying somewhere that json outout just like the text output is not stable seems like a good starting point.

@miekg miekg closed this May 1, 2026
@miekg miekg deleted the miek/26/apr30do/normalizejson branch May 1, 2026 04:37
@psviderski

Copy link
Copy Markdown
Owner

just like the text output is not stable seems like a good starting point.

What do you mean when you say JSON is not stable? What scenario do you have in your mind you want to warn about?

@miekg

miekg commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants