Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate builder objects for building API objects #7

Closed
wafflespeanut opened this issue May 29, 2019 · 1 comment
Closed

Generate builder objects for building API objects #7

wafflespeanut opened this issue May 29, 2019 · 1 comment
Assignees
Labels
enhancement New feature or request legendary A lot of work is involved!

Comments

@wafflespeanut
Copy link
Collaborator

wafflespeanut commented May 29, 2019

The first step to generating client code would be to build type-safe compile-time checked API objects. This is done by leveraging PhantomData and unit types. An example would be:

struct MissingName;
struct MissingNamespace;
struct NamePresent;
struct NamespacePresent;

struct Get;

#[repr(transparent)]    // for safely transmuting
#[derive(Debug, Clone)]
pub struct ConfigMapFactory<Name, Namespace> {
    inner: ConfigMapBuilder,
    _param_name: PhantomData<Name>,
    _param_namespace: PhantomData<Namespace>,
}

#[derive(Default, Debug, Clone)]
struct ConfigMapBuilder {
    inner: ConfigMap,
    name: Option<String>,
    namespace: Option<String>,
}

impl ConfigMap {
    fn get() -> ConfigMapFactory<MissingName, MissingNamespace> {
        ConfigMapFactory {
            inner: Default::default(),
            _param_name: PhantomData,
            _param_namespace: PhantomData,
        }
    }
}

impl<A> ConfigMapFactory<MissingName, A> {
    pub fn name(mut self, name: &str) -> ConfigMapFactory<NamePresent, A> {
        self.inner.name = Some(name.into());
        unsafe { mem::transmute(self) }
    }
}

Then, we'll add an impl for the actual API request only when the type has NamePresent and NamespacePresent (we do this for all required parameters and fields). This means rustc won't compile the code when the user forgets some required entity.

Anyway, the focus for this issue would be to generate all of this stuff for all API objects (taking into account of required parameters and fields).

@wafflespeanut wafflespeanut added the enhancement New feature or request label May 29, 2019
@wafflespeanut wafflespeanut self-assigned this May 29, 2019
@wafflespeanut wafflespeanut added epic A fair amount of work is involved legendary A lot of work is involved! and removed epic A fair amount of work is involved labels May 29, 2019
@wafflespeanut
Copy link
Collaborator Author

I had to do some workarounds for some edge cases, for which I've dropped FIXMEs, but aside that, IT IS DONE - the generated docs are live!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request legendary A lot of work is involved!
Projects
None yet
Development

No branches or pull requests

1 participant