Skip to content

Conversation

@bhelx
Copy link
Contributor

@bhelx bhelx commented Nov 4, 2024

See dylibso/xtp-bindgen#18

Required Vs Nullable

Picking up where #10 left off . How i am implementing required vs nullable

This schema:

    RequiredVsNullable:
      description: A weird little object
      required:
        - aRequiredNotNullableBoolean
        - aRequiredNullableBoolean
      properties:
        aRequiredNotNullableBoolean:
          type: boolean
          description: A required, not-nullable boolean prop
        aRequiredNullableBoolean:
          type: boolean
          description: A required ,nullable boolean prop
          nullable: true
        aNotRequiredNotNullableBoolean:
          type: boolean
          description: A not-required, not-nullable boolean prop
        aNotRequiredNullableBoolean:
          type: boolean
          description: A not-required, nullable boolean prop
          nullable: true

Results in this object:

    #[derive(
        Default, serde::Serialize, serde::Deserialize, extism_pdk::FromBytes, extism_pdk::ToBytes,
    )]
    #[encoding(Json)]
    pub struct RequiredVsNullable {
        /// A not-nullable boolean prop
        #[serde(rename = "aNotRequiredNotNullableBoolean")]
        #[serde(skip_serializing_if = "Option::is_none")]
        #[serde(default = "panic_if_key_missing")]
        pub a_not_required_not_nullable_boolean: Option<bool>,

        /// A nullable boolean prop
        #[serde(rename = "aNotRequiredNullableBoolean")]
        #[serde(skip_serializing_if = "Option::is_none")]
        #[serde(default = "panic_if_key_missing")]
        pub a_not_required_nullable_boolean: Option<bool>,

        /// A not-nullable boolean prop
        #[serde(rename = "aRequiredNotNullableBoolean")]
        pub a_required_not_nullable_boolean: bool,

        /// A nullable boolean prop
        #[serde(rename = "aRequiredNullableBoolean")]
        #[serde(default)]
        pub a_required_nullable_boolean: Option<bool>,
    }

// we're just exposing the serde values directly for backwards compat
return optionalize("std::collections::HashMap<String, serde_json::Value>")
}
return "i64";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: we have a type for i64 but don't yet support it because of json limitations. i have an issue filed for this.

@bhelx bhelx changed the title Refactor for bingen updates Refactor for bindgen updates Nov 4, 2024
* Refactor: see dylibso/xtp-bindgen#18
* Support nullable vs required
return optionalize(`types::${helpers.capitalize((type as EnumType).name)}`)
case 'map':
const { keyType, valueType } = type as MapType
return optionalize(`std::collections::HashMap<${toRustTypeX(keyType)}, ${toRustTypeX(valueType)}>`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to use serde_json::Value::Map instead of HashMap while we're here. (serde_json::Map gives the plugin author the ability to preserve key order.)

@zshipko zshipko linked an issue Nov 8, 2024 that may be closed by this pull request
@zshipko
Copy link

zshipko commented Nov 21, 2024

Closing since this work was included in #18

@zshipko zshipko closed this Nov 21, 2024
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.

Improve handling of optional buffers

4 participants