Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/data/tests/can_override_disallowed_types/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[typeshare]
struct DisallowedType {
#[typeshare(typescript(type = "bigint"))]
disallowed_type: u64,
#[typeshare(typescript(type = "number"))]
another_disallowed_type: i64,
#[typeshare(typescript(type = "string"))]
#[serde(with = "my_string_serde_impl")]
disallowed_type_serde_with: u64,
}
9 changes: 9 additions & 0 deletions core/data/tests/can_override_disallowed_types/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package proto

import "encoding/json"

type DisallowedType struct {
DisallowedType uint64 `json:"disallowed_type"`
AnotherDisallowedType int64 `json:"another_disallowed_type"`
DisallowedTypeSerdeWith uint64 `json:"disallowed_type_serde_with"`
}
12 changes: 12 additions & 0 deletions core/data/tests/can_override_disallowed_types/output.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.agilebits.onepassword

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName

@Serializable
data class DisallowedType (
val disallowed_type: ULong,
val another_disallowed_type: Long,
val disallowed_type_serde_with: ULong
)

19 changes: 19 additions & 0 deletions core/data/tests/can_override_disallowed_types/output.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.agilebits

package object onepassword {

type UByte = Byte
type UShort = Short
type UInt = Int
type ULong = Int

}
package onepassword {

case class DisallowedType (
disallowed_type: ULong,
another_disallowed_type: Long,
disallowed_type_serde_with: ULong
)

}
13 changes: 13 additions & 0 deletions core/data/tests/can_override_disallowed_types/output.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

public struct DisallowedType: Codable {
public let disallowed_type: UInt64
public let another_disallowed_type: Int64
public let disallowed_type_serde_with: UInt64

public init(disallowed_type: UInt64, another_disallowed_type: Int64, disallowed_type_serde_with: UInt64) {
self.disallowed_type = disallowed_type
self.another_disallowed_type = another_disallowed_type
self.disallowed_type_serde_with = disallowed_type_serde_with
}
}
6 changes: 6 additions & 0 deletions core/data/tests/can_override_disallowed_types/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface DisallowedType {
disallowed_type: bigint;
another_disallowed_type: number;
disallowed_type_serde_with: string;
}

15 changes: 15 additions & 0 deletions core/data/tests/can_override_pointer_sized_types/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[typeshare]
struct PointerSizedType {
#[typeshare(kotlin(type = "ULong"))]
#[typeshare(scala(type = "ULong"))]
#[typeshare(swift(type = "UInt64"))]
#[typeshare(typescript(type = "number"))]
#[typeshare(go(type = "uint64"))]
unsigned: usize,
#[typeshare(kotlin(type = "Long"))]
#[typeshare(scala(type = "Long"))]
#[typeshare(swift(type = "Int64"))]
#[typeshare(typescript(type = "number"))]
#[typeshare(go(type = "int64"))]
signed: isize,
}
8 changes: 8 additions & 0 deletions core/data/tests/can_override_pointer_sized_types/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package proto

import "encoding/json"

type PointerSizedType struct {
Unsigned uint64 `json:"unsigned"`
Signed int64 `json:"signed"`
}
11 changes: 11 additions & 0 deletions core/data/tests/can_override_pointer_sized_types/output.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.agilebits.onepassword

import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName

@Serializable
data class PointerSizedType (
val unsigned: ULong,
val signed: Long
)

18 changes: 18 additions & 0 deletions core/data/tests/can_override_pointer_sized_types/output.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.agilebits

package object onepassword {

type UByte = Byte
type UShort = Short
type UInt = Int
type ULong = Int

}
package onepassword {

case class PointerSizedType (
unsigned: ULong,
signed: Long
)

}
11 changes: 11 additions & 0 deletions core/data/tests/can_override_pointer_sized_types/output.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

public struct PointerSizedType: Codable {
public let unsigned: UInt64
public let signed: Int64

public init(unsigned: UInt64, signed: Int64) {
self.unsigned = unsigned
self.signed = signed
}
}
5 changes: 5 additions & 0 deletions core/data/tests/can_override_pointer_sized_types/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface PointerSizedType {
unsigned: number;
signed: number;
}

10 changes: 7 additions & 3 deletions core/src/language/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ impl Language for Go {
| SpecialRustType::U8
| SpecialRustType::U16
| SpecialRustType::I32
| SpecialRustType::I16
| SpecialRustType::ISize
| SpecialRustType::USize => "int".into(),
| SpecialRustType::I16 => "int".into(),
SpecialRustType::U32 => "uint32".into(),
SpecialRustType::I54 | SpecialRustType::I64 => "int64".into(),
SpecialRustType::U53 | SpecialRustType::U64 => "uint64".into(),
Expand All @@ -169,6 +167,12 @@ impl Language for Go {
self.add_import("time");
"time.Time".into()
}
SpecialRustType::ISize | SpecialRustType::USize => {
panic!(
"Pointer-sized types require an explicit output type. \
See: https://1password.github.io/typeshare/usage/annotations.html#special-note-on-pointer-sized-types for more information."
)
}
})
}

Expand Down
10 changes: 8 additions & 2 deletions core/src/language/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ impl Language for Kotlin {
// https://kotlinlang.org/docs/basic-types.html#integer-types
SpecialRustType::I8 => "Byte".into(),
SpecialRustType::I16 => "Short".into(),
SpecialRustType::ISize | SpecialRustType::I32 => "Int".into(),
SpecialRustType::I32 => "Int".into(),
SpecialRustType::I54 | SpecialRustType::I64 => "Long".into(),
// https://kotlinlang.org/docs/basic-types.html#unsigned-integers
SpecialRustType::U8 => "UByte".into(),
SpecialRustType::U16 => "UShort".into(),
SpecialRustType::USize | SpecialRustType::U32 => "UInt".into(),
SpecialRustType::U32 => "UInt".into(),
SpecialRustType::U53 | SpecialRustType::U64 => "ULong".into(),
SpecialRustType::Bool => "Boolean".into(),
SpecialRustType::F32 => "Float".into(),
Expand All @@ -95,6 +95,12 @@ impl Language for Kotlin {
special_ty.to_string(),
))
}
SpecialRustType::ISize | SpecialRustType::USize => {
panic!(
"Pointer-sized types require an explicit output type. \
See: https://1password.github.io/typeshare/usage/annotations.html#special-note-on-pointer-sized-types for more information."
)
}
})
}

Expand Down
10 changes: 8 additions & 2 deletions core/src/language/scala.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ impl Language for Scala {
SpecialRustType::String | SpecialRustType::Char => "String".into(),
SpecialRustType::I8 => "Byte".into(),
SpecialRustType::I16 => "Short".into(),
SpecialRustType::ISize | SpecialRustType::I32 => "Int".into(),
SpecialRustType::I32 => "Int".into(),
SpecialRustType::I54 | SpecialRustType::I64 => "Long".into(),
// Scala does not support unsigned integers, so upcast it to the closest one
SpecialRustType::U8 => "UByte".into(),
SpecialRustType::U16 => "UShort".into(),
SpecialRustType::USize | SpecialRustType::U32 => "UInt".into(),
SpecialRustType::U32 => "UInt".into(),
SpecialRustType::U53 | SpecialRustType::U64 => "ULong".into(),
SpecialRustType::Bool => "Boolean".into(),
SpecialRustType::F32 => "Float".into(),
Expand All @@ -118,6 +118,12 @@ impl Language for Scala {
special_ty.to_string(),
))
}
SpecialRustType::ISize | SpecialRustType::USize => {
panic!(
"Pointer-sized types require an explicit output type. \
See: https://1password.github.io/typeshare/usage/annotations.html#special-note-on-pointer-sized-types for more information."
)
}
})
}

Expand Down
8 changes: 6 additions & 2 deletions core/src/language/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ impl Language for Swift {
SpecialRustType::U8 => "UInt8".into(),
SpecialRustType::I16 => "Int16".into(),
SpecialRustType::U16 => "UInt16".into(),
SpecialRustType::USize => "UInt".into(),
SpecialRustType::ISize => "Int".into(),
SpecialRustType::I32 => "Int32".into(),
SpecialRustType::U32 => "UInt32".into(),
SpecialRustType::I54 | SpecialRustType::I64 => "Int64".into(),
Expand All @@ -221,6 +219,12 @@ impl Language for Swift {
special_ty.to_string(),
))
}
SpecialRustType::ISize | SpecialRustType::USize => {
panic!(
"Pointer-sized types require an explicit output type. \
See: https://1password.github.io/typeshare/usage/annotations.html#special-note-on-pointer-sized-types for more information."
)
}
})
}

Expand Down
16 changes: 11 additions & 5 deletions core/src/language/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,17 @@ export const ReplacerFunc = (key: string, value: unknown): unknown => {{
| SpecialRustType::F32
| SpecialRustType::F64 => Ok("number".into()),
SpecialRustType::Bool => Ok("boolean".into()),
SpecialRustType::U64
| SpecialRustType::I64
| SpecialRustType::ISize
| SpecialRustType::USize => {
panic!("64 bit types not allowed in Typeshare")
SpecialRustType::U64 | SpecialRustType::I64 => {
panic!(
"64 bit integer types require an explicit output type for TypeScript. \
See: https://1password.github.io/typeshare/usage/annotations.html#special-note-on-64-bit-integer-types for more information."
)
}
SpecialRustType::ISize | SpecialRustType::USize => {
panic!(
"Pointer-sized types require an explicit output type. \
See: https://1password.github.io/typeshare/usage/annotations.html#special-note-on-pointer-sized-types for more information."
)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/rust_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,14 @@ impl TryFrom<&syn::Type> for RustType {
"u16" => Self::Special(SpecialRustType::U16),
"u32" => Self::Special(SpecialRustType::U32),
"U53" => Self::Special(SpecialRustType::U53),
"u64" | "i64" | "usize" | "isize" => {
return Err(RustTypeParseError::UnsupportedType(vec![id]))
}
"u64" => Self::Special(SpecialRustType::U64),
"usize" => Self::Special(SpecialRustType::USize),
"i8" => Self::Special(SpecialRustType::I8),
"i16" => Self::Special(SpecialRustType::I16),
"i32" => Self::Special(SpecialRustType::I32),
"I54" => Self::Special(SpecialRustType::I54),
"i64" => Self::Special(SpecialRustType::I64),
"isize" => Self::Special(SpecialRustType::ISize),
"f32" => Self::Special(SpecialRustType::F32),
"f64" => Self::Special(SpecialRustType::F64),
_ => {
Expand Down
63 changes: 0 additions & 63 deletions core/tests/agnostic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use typeshare_core::{
context::{ParseContext, ParseFileContext},
language::{CrateTypes, Language, TypeScript},
parser::{self, ParseError},
rust_types::RustTypeParseError,
ProcessInputError,
};
/// Parse and generate types for a single Rust input file.
Expand Down Expand Up @@ -36,68 +35,6 @@ pub fn process_input(
Ok(())
}

mod blocklisted_types {
use std::collections::HashMap;

use super::*;

fn assert_type_is_blocklisted(ty: &str, blocklisted_type: &str) {
let source = format!(
r##"
#[typeshare]
#[serde(default, rename_all = "camelCase")]
pub struct Foo {{
pub bar: {ty},
}}
"##,
ty = ty
);

let mut out: Vec<u8> = Vec::new();
assert!(matches!(
process_input(&source, &mut TypeScript::default(), &HashMap::new(), &mut out),
Err(ProcessInputError::ParseError(
ParseError::RustTypeParseError(RustTypeParseError::UnsupportedType(contents))
)) if contents == vec![blocklisted_type.to_owned()]
));
}

#[test]
fn test_i64_blocklisted_struct() {
assert_type_is_blocklisted("i64", "i64");
}

#[test]
fn test_u64_blocklisted_struct() {
assert_type_is_blocklisted("u64", "u64");
}

#[test]
fn test_isize_blocklisted_struct() {
assert_type_is_blocklisted("isize", "isize");
}

#[test]
fn test_usize_blocklisted_in_struct() {
assert_type_is_blocklisted("usize", "usize");
}

#[test]
fn test_optional_blocklisted_struct() {
assert_type_is_blocklisted("Option<i64>", "i64");
}

#[test]
fn test_vec_blocklisted_struct() {
assert_type_is_blocklisted("Vec<i64>", "i64");
}

#[test]
fn test_hashmap_blocklisted_struct() {
assert_type_is_blocklisted("HashMap<String, i64>", "i64");
}
}

mod serde_attributes_on_enums {
use std::collections::HashMap;

Expand Down
2 changes: 2 additions & 0 deletions core/tests/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ tests! {
python
];
can_override_types: [swift, kotlin, scala, typescript, go];
can_override_disallowed_types: [swift, kotlin, scala, typescript, go];
can_override_pointer_sized_types: [swift, kotlin, scala, typescript, go];

/// Structs
can_generate_simple_struct_with_a_comment: [kotlin, swift, typescript, scala, go, python];
Expand Down
Loading