0

I have Context objects - essentially a join record between a Cluster and a User - in a kubernetes config yaml ( ~/.kube/config ) i'd like to parse in to a struct that looks something like this:

struct Context {
  name: String,
  cluster: Cluster,
  user: User
}

struct Cluster {
  server: String
  // ..
}

struct User {
  name: String,
  // ..
}

the yaml itself encodes these in to separate arrays, e.g.:

contexts:
- name: some_context
  context:
    cluster: some_cluster
    user: some_user
clusters:
- name: some_cluster
  cluster:
    // many other, important fields ..
users:
- name: some_user
  user:
    // many other, important fields ..

i have no idea where to even begin with this - it seems as though i will need to have access to an interim representation of the document before i could "look ahead" to parse some of the complex structures in other parts of the document in to these structs external to Context

I've found the go-to kubernetes client crate is parsing this exact file, but it shies away from doing this and just provides the "raw" string keys

https://github.com/clux/kube-rs/blob/8c42f78ef86950baa17440fab992e39a12c5811b/kube/src/config/file_config.rs#L128

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Context {
    pub cluster: String, // ideally cluster: Cluster
    pub user: String, // ideally user: User
    pub namespace: Option<String>,
    pub extensions: Option<Vec<NamedExtension>>,
}

( comments in above snippet by me )

is there an example for how to do this serde?

pgn
  • 669
  • 1
  • 6
  • 16

1 Answers1

0

I have thought about this a little longer and this may not be in scope for serde. I definitely found no way to do it currently.

Instead, I implemented a TryFrom between the low-level types with string id references, coming straight from serde, and my higher-level type with these references resolved.

pgn
  • 669
  • 1
  • 6
  • 16