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

Write out types in topological order #62

Merged
merged 10 commits into from
Mar 30, 2023
10 changes: 5 additions & 5 deletions core/data/tests/can_generate_generic_enum/output.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ package com.agilebits.onepassword
import androidx.compose.runtime.NoLiveLiterals
import kotlinx.serialization.*

@Serializable
data class StructUsingGenericEnum (
val enum_field: GenericEnum<String, Short>
)

@Serializable
sealed class GenericEnum<A, B> {
@Serializable
Expand All @@ -24,6 +19,11 @@ sealed class GenericEnum<A, B> {
data class VariantB<A, B>(val content: B): GenericEnum<A, B>()
}

@Serializable
data class StructUsingGenericEnum (
val enum_field: GenericEnum<String, Short>
)

@Serializable
sealed class GenericEnumUsingGenericEnum<T> {
@Serializable
Expand Down
16 changes: 8 additions & 8 deletions core/data/tests/can_generate_generic_enum/output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@

import Foundation

public struct CoreStructUsingGenericEnum: Codable {
public let enum_field: CoreGenericEnum<String, Int16>

public init(enum_field: CoreGenericEnum<String, Int16>) {
self.enum_field = enum_field
}
}

public enum CoreGenericEnum<A: Codable, B: Codable>: Codable {
case variantA(A)
case variantB(B)
Expand Down Expand Up @@ -57,6 +49,14 @@ public enum CoreGenericEnum<A: Codable, B: Codable>: Codable {
}
}

public struct CoreStructUsingGenericEnum: Codable {
public let enum_field: CoreGenericEnum<String, Int16>

public init(enum_field: CoreGenericEnum<String, Int16>) {
self.enum_field = enum_field
}
}

public enum CoreGenericEnumUsingGenericEnum<T: Codable>: Codable {
case variantC(CoreGenericEnum<T, T>)
case variantD(CoreGenericEnum<String, [String: T]>)
Expand Down
8 changes: 4 additions & 4 deletions core/data/tests/can_generate_generic_enum/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
Generated by typeshare 1.0.0
*/

export interface StructUsingGenericEnum {
enum_field: GenericEnum<string, number>;
}

export type GenericEnum<A, B> =
| { type: "VariantA", content: A }
| { type: "VariantB", content: B };

export interface StructUsingGenericEnum {
enum_field: GenericEnum<string, number>;
}

export type GenericEnumUsingGenericEnum<T> =
| { type: "VariantC", content: GenericEnum<T, T> }
| { type: "VariantD", content: GenericEnum<string, Record<string, T>> }
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions core/data/tests/orders_types/output.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions core/data/tests/orders_types/output.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Generated by typeshare 1.0.0
*/

@file:NoLiveLiterals

package com.agilebits.onepassword

import androidx.compose.runtime.NoLiveLiterals
import kotlinx.serialization.*

5 changes: 5 additions & 0 deletions core/data/tests/orders_types/output.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
Generated by typeshare 1.0.0
*/

import Foundation
21 changes: 17 additions & 4 deletions core/src/language/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::io::Write;

use crate::parser::ParsedData;
use crate::rename::RenameExt;
use crate::rust_types::{RustTypeFormatError, SpecialRustType};
use crate::rust_types::{RustThing, RustTypeFormatError, SpecialRustType};
use crate::{
language::Language,
rust_types::{RustEnum, RustEnumVariant, RustField, RustStruct, RustTypeAlias},
topsort::topsort,
};
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -36,16 +37,28 @@ impl Language for Go {

self.begin_file(w)?;

let mut things: Vec<RustThing> = vec![];

for a in &data.aliases {
self.write_type_alias(w, a)?;
things.push(RustThing::TypeAlias(a))
}

for s in &data.structs {
self.write_struct(w, s)?;
things.push(RustThing::Struct(s))
}

for e in &data.enums {
self.write_enum(w, e, &types_mapping_to_struct)?;
things.push(RustThing::Enum(e))
}

let sorted = topsort(things.iter().collect());

for &thing in &sorted {
match thing {
RustThing::Enum(e) => self.write_enum(w, e, &types_mapping_to_struct)?,
RustThing::Struct(s) => self.write_struct(w, s)?,
RustThing::TypeAlias(a) => self.write_type_alias(w, a)?,
}
}

self.end_file(w)?;
Expand Down
21 changes: 17 additions & 4 deletions core/src/language/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
parser::ParsedData,
rust_types::{Id, RustEnum, RustEnumVariant, RustStruct, RustTypeAlias},
rust_types::{Id, RustEnum, RustEnumVariant, RustStruct, RustThing, RustTypeAlias},
topsort::topsort,
};
use itertools::Itertools;
use std::{collections::HashMap, io::Write};
Expand Down Expand Up @@ -31,16 +32,28 @@ pub trait Language {
) -> std::io::Result<()> {
self.begin_file(writable)?;

let mut things: Vec<RustThing> = vec![];

for a in &data.aliases {
self.write_type_alias(writable, a)?;
things.push(RustThing::TypeAlias(a))
}

for s in &data.structs {
self.write_struct(writable, s)?;
things.push(RustThing::Struct(s))
}

for e in &data.enums {
self.write_enum(writable, e)?;
things.push(RustThing::Enum(e))
}

let sorted = topsort(things.iter().collect());

for &thing in &sorted {
match thing {
RustThing::Enum(e) => self.write_enum(writable, e)?,
RustThing::Struct(s) => self.write_struct(writable, s)?,
RustThing::TypeAlias(a) => self.write_type_alias(writable, a)?,
}
}

self.end_file(writable)?;
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod language;
pub mod parser;
/// Codifying Rust types and how they convert to various languages.
pub mod rust_types;
mod topsort;

#[derive(Debug, Error)]
#[allow(missing_docs)]
Expand Down
23 changes: 15 additions & 8 deletions core/src/rust_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{collections::HashMap, convert::TryFrom};
use thiserror::Error;

/// Identifier used in Rust structs, enums, and fields. It includes the `original` name and the `renamed` value after the transformation based on `serde` attributes.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Id {
/// The original identifier name
pub original: String,
Expand All @@ -25,7 +25,7 @@ impl std::fmt::Display for Id {
}

/// Rust struct.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct RustStruct {
/// The identifier for the struct.
pub id: Id,
Expand All @@ -45,7 +45,7 @@ pub struct RustStruct {
/// ```
/// pub struct MasterPassword(String);
/// ```
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct RustTypeAlias {
/// The identifier for the alias.
pub id: Id,
Expand All @@ -58,7 +58,7 @@ pub struct RustTypeAlias {
}

/// Rust field definition.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct RustField {
/// Identifier for the field.
pub id: Id,
Expand Down Expand Up @@ -383,7 +383,7 @@ impl SpecialRustType {
}

/// Parsed information about a Rust enum definition
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum RustEnum {
/// A unit enum
///
Expand Down Expand Up @@ -433,7 +433,7 @@ impl RustEnum {
}

/// Enum information shared among different enum types
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct RustEnumShared {
/// The enum's ident
pub id: Id,
Expand All @@ -453,7 +453,7 @@ pub struct RustEnumShared {
}

/// Parsed information about a Rust enum variant
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum RustEnumVariant {
/// A unit variant
Unit(RustEnumVariantShared),
Expand Down Expand Up @@ -485,10 +485,17 @@ impl RustEnumVariant {
}

/// Variant information shared among different variant types
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct RustEnumVariantShared {
/// The variant's ident
pub id: Id,
/// Comments applied to the variant
pub comments: Vec<String>,
}

#[derive(Debug, PartialEq)]
pub enum RustThing<'a> {
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
Enum(&'a RustEnum),
TypeAlias(&'a RustTypeAlias),
Struct(&'a RustStruct),
}
Loading