Skip to content

Commit

Permalink
Deterministic output
Browse files Browse the repository at this point in the history
I want to use a parallel iterator while walking the input folders however in order to maintain a deterministic output I'll have to keep the crate to parsed data sorted along with the types within each parsed data. Consequently the tests have been updated since they did not have this sorting before.
  • Loading branch information
darrell-roberts authored and Cerulan Lumina committed Jul 22, 2024
1 parent ff2b13b commit ecc8ce7
Show file tree
Hide file tree
Showing 41 changed files with 400 additions and 345 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ serde = { version = "1", features = ["derive"] }
toml = "0.8"
typeshare-core = { path = "../core", version = "1.10.0-beta.6" }
anyhow = "1"
indexmap = "2"
5 changes: 2 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use args::{
use clap::ArgMatches;
use config::Config;
use ignore::{overrides::OverrideBuilder, types::TypesBuilder, WalkBuilder};
use indexmap::IndexMap;
use parse::{all_types, parse_input, parser_inputs};
use rayon::iter::ParallelBridge;
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
#[cfg(feature = "go")]
use typeshare_core::language::Go;
use typeshare_core::{
Expand Down Expand Up @@ -226,7 +225,7 @@ fn override_configuration(mut config: Config, options: &ArgMatches) -> Config {
}

/// Prints out all parsing errors if any and returns Err.
fn check_parse_errors(parsed_crates: &IndexMap<CrateName, ParsedData>) -> anyhow::Result<()> {
fn check_parse_errors(parsed_crates: &BTreeMap<CrateName, ParsedData>) -> anyhow::Result<()> {
let mut errors_encountered = false;
for data in parsed_crates
.values()
Expand Down
13 changes: 6 additions & 7 deletions cli/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Source file parsing.
use anyhow::Context;
use ignore::WalkBuilder;
use indexmap::IndexMap;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::{
collections::{hash_map::Entry, HashMap},
collections::{hash_map::Entry, BTreeMap, HashMap},
path::PathBuf,
};
use typeshare_core::{
Expand Down Expand Up @@ -67,7 +66,7 @@ fn output_file_name(language_type: SupportedLanguage, crate_name: &CrateName) ->

/// Collect all the typeshared types into a mapping of crate names to typeshared types. This
/// mapping is used to lookup and generated import statements for generated files.
pub fn all_types(file_mappings: &IndexMap<CrateName, ParsedData>) -> CrateTypes {
pub fn all_types(file_mappings: &BTreeMap<CrateName, ParsedData>) -> CrateTypes {
file_mappings
.iter()
.map(|(crate_name, parsed_data)| (crate_name, parsed_data.type_names.clone()))
Expand All @@ -93,12 +92,12 @@ pub fn parse_input(
ignored_types: &[&str],
multi_file: bool,
target_os: Option<String>,
) -> anyhow::Result<IndexMap<CrateName, ParsedData>> {
) -> anyhow::Result<BTreeMap<CrateName, ParsedData>> {
inputs
.into_par_iter()
.try_fold(
IndexMap::new,
|mut parsed_crates: IndexMap<CrateName, ParsedData>,
BTreeMap::new,
|mut parsed_crates: BTreeMap<CrateName, ParsedData>,
ParserInput {
file_path,
file_name,
Expand Down Expand Up @@ -126,7 +125,7 @@ pub fn parse_input(
Ok(parsed_crates)
},
)
.try_reduce(IndexMap::new, |mut file_maps, parsed_crates| {
.try_reduce(BTreeMap::new, |mut file_maps, parsed_crates| {
for (crate_name, parsed_data) in parsed_crates {
file_maps.entry(crate_name).or_default().add(parsed_data);
}
Expand Down
11 changes: 5 additions & 6 deletions cli/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
use crate::args::{ARG_OUTPUT_FILE, ARG_OUTPUT_FOLDER};
use anyhow::Context;
use clap::ArgMatches;
use indexmap::IndexMap;
use std::{
collections::HashMap,
collections::{BTreeMap, HashMap},
fs,
path::{Path, PathBuf},
};
Expand All @@ -17,7 +16,7 @@ use typeshare_core::{
pub fn write_generated(
options: ArgMatches,
lang: Box<dyn Language>,
crate_parsed_data: IndexMap<CrateName, ParsedData>,
crate_parsed_data: BTreeMap<CrateName, ParsedData>,
import_candidates: CrateTypes,
) -> Result<(), anyhow::Error> {
let output_folder = options.value_of(ARG_OUTPUT_FOLDER);
Expand All @@ -36,7 +35,7 @@ pub fn write_generated(
fn write_multiple_files(
mut lang: Box<dyn Language>,
output_folder: &str,
crate_parsed_data: IndexMap<CrateName, ParsedData>,
crate_parsed_data: BTreeMap<CrateName, ParsedData>,
import_candidates: CrateTypes,
) -> Result<(), anyhow::Error> {
for (_crate_name, parsed_data) in crate_parsed_data {
Expand Down Expand Up @@ -84,10 +83,10 @@ fn check_write_file(outfile: &PathBuf, output: Vec<u8>) -> anyhow::Result<()> {
fn write_single_file(
mut lang: Box<dyn Language>,
file_name: &str,
mut crate_parsed_data: IndexMap<CrateName, ParsedData>,
mut crate_parsed_data: BTreeMap<CrateName, ParsedData>,
) -> Result<(), anyhow::Error> {
let parsed_data = crate_parsed_data
.shift_remove(&SINGLE_FILE_CRATE_NAME)
.remove(&SINGLE_FILE_CRATE_NAME)
.context("Could not get parsed data for single file output")?;

let mut output = Vec::new();
Expand Down
4 changes: 2 additions & 2 deletions core/data/tests/can_recognize_types_inside_modules/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import "encoding/json"
type A struct {
Field uint32 `json:"field"`
}
type ABC struct {
type AB struct {
Field uint32 `json:"field"`
}
type AB struct {
type ABC struct {
Field uint32 `json:"field"`
}
type OutsideOfModules struct {
Expand Down
4 changes: 2 additions & 2 deletions core/data/tests/can_recognize_types_inside_modules/output.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ data class A (
)

@Serializable
data class ABC (
data class AB (
val field: UInt
)

@Serializable
data class AB (
data class ABC (
val field: UInt
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ case class A (
field: UInt
)

case class ABC (
case class AB (
field: UInt
)

case class AB (
case class ABC (
field: UInt
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public struct A: Codable {
}
}

public struct ABC: Codable {
public struct AB: Codable {
public let field: UInt32

public init(field: UInt32) {
self.field = field
}
}

public struct AB: Codable {
public struct ABC: Codable {
public let field: UInt32

public init(field: UInt32) {
Expand Down
4 changes: 2 additions & 2 deletions core/data/tests/can_recognize_types_inside_modules/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ export interface A {
field: number;
}

export interface ABC {
export interface AB {
field: number;
}

export interface AB {
export interface ABC {
field: number;
}

Expand Down
6 changes: 3 additions & 3 deletions core/data/tests/excluded_by_target_os/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package proto

import "encoding/json"

type SomeEnum string
const (
)
type TestEnum string
const (
TestEnumVariant1 TestEnum = "Variant1"
TestEnumVariant5 TestEnum = "Variant5"
)
type SomeEnum string
const (
)
8 changes: 4 additions & 4 deletions core/data/tests/excluded_by_target_os/output.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package com.agilebits.onepassword
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName

@Serializable
enum class SomeEnum(val string: String) {
}

@Serializable
enum class TestEnum(val string: String) {
@SerialName("Variant1")
Expand All @@ -11,7 +15,3 @@ enum class TestEnum(val string: String) {
Variant5("Variant5"),
}

@Serializable
enum class SomeEnum(val string: String) {
}

12 changes: 6 additions & 6 deletions core/data/tests/excluded_by_target_os/output.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package com.agilebits

package onepassword {

sealed trait SomeEnum {
def serialName: String
}
object SomeEnum {
}

sealed trait TestEnum {
def serialName: String
}
Expand All @@ -14,10 +20,4 @@ object TestEnum {
}
}

sealed trait SomeEnum {
def serialName: String
}
object SomeEnum {
}

}
6 changes: 3 additions & 3 deletions core/data/tests/excluded_by_target_os/output.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Foundation

public enum SomeEnum: String, Codable {
}

public enum TestEnum: String, Codable {
case variant1 = "Variant1"
case variant5 = "Variant5"
}

public enum SomeEnum: String, Codable {
}
6 changes: 3 additions & 3 deletions core/data/tests/excluded_by_target_os/output.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export enum SomeEnum {
}

export enum TestEnum {
Variant1 = "Variant1",
Variant5 = "Variant5",
}

export enum SomeEnum {
}

10 changes: 5 additions & 5 deletions core/data/tests/generate_types_with_keywords/output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ public struct `catch`: Codable {
}
}

public enum `throws`: String, Codable {
case `case`
case `default`
}

public enum `switch`: Codable {
case `default`(`catch`)

Expand Down Expand Up @@ -49,3 +44,8 @@ public enum `switch`: Codable {
}
}
}

public enum `throws`: String, Codable {
case `case`
case `default`
}
6 changes: 3 additions & 3 deletions core/data/tests/orders_types/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type B struct {
type C struct {
DependsOn B `json:"dependsOn"`
}
type E struct {
DependsOn D `json:"dependsOn"`
}
type D struct {
DependsOn C `json:"dependsOn"`
AlsoDependsOn *E `json:"alsoDependsOn,omitempty"`
}
type E struct {
DependsOn D `json:"dependsOn"`
}
10 changes: 5 additions & 5 deletions core/data/tests/orders_types/output.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ data class C (
)

@Serializable
data class D (
val dependsOn: C,
val alsoDependsOn: E? = null
data class E (
val dependsOn: D
)

@Serializable
data class E (
val dependsOn: D
data class D (
val dependsOn: C,
val alsoDependsOn: E? = null
)

16 changes: 8 additions & 8 deletions core/data/tests/orders_types/output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ public struct C: Codable {
}
}

public struct D: Codable {
public let dependsOn: C
public let alsoDependsOn: E?
public struct E: Codable {
public let dependsOn: D

public init(dependsOn: C, alsoDependsOn: E?) {
public init(dependsOn: D) {
self.dependsOn = dependsOn
self.alsoDependsOn = alsoDependsOn
}
}

public struct E: Codable {
public let dependsOn: D
public struct D: Codable {
public let dependsOn: C
public let alsoDependsOn: E?

public init(dependsOn: D) {
public init(dependsOn: C, alsoDependsOn: E?) {
self.dependsOn = dependsOn
self.alsoDependsOn = alsoDependsOn
}
}
Loading

0 comments on commit ecc8ce7

Please sign in to comment.