Skip to content

Commit

Permalink
Merge pull request #204 from darrell-roberts/darrell/update-for-empty…
Browse files Browse the repository at this point in the history
…-target-os

update for no target-os
  • Loading branch information
kareid authored Nov 12, 2024
2 parents da98c89 + d1b2b06 commit 41de151
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct SwiftParams {
pub prefix: String,
pub default_decorators: Vec<String>,
pub default_generic_constraints: Vec<String>,
/// The contraints to apply to `CodableVoid`.
/// The constraints to apply to `CodableVoid`.
pub codablevoid_constraints: Vec<String>,
pub type_mappings: HashMap<String, String>,
}
Expand All @@ -55,7 +55,7 @@ pub struct GoParams {
pub type_mappings: HashMap<String, String>,
}

/// The paramters that are used to configure the behaviour of typeshare
/// The parameters that are used to configure the behaviour of typeshare
/// from the configuration file `typeshare.toml`
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
#[serde(default)]
Expand Down
11 changes: 11 additions & 0 deletions core/data/tests/excluded_by_target_os/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,14 @@ pub struct AndroidExcluded;
#[typeshare]
#[cfg(all(feature = "my-feature", not(target_os = "ios")))]
pub struct NestedNotTarget1;

/// A struct with no target_os. Should be generated when
/// we use --target-os.
#[typeshare]
pub struct AlwaysAccept;

#[typeshare]
pub enum AlwaysAcceptEnum {
Variant1,
Variant2,
}
9 changes: 9 additions & 0 deletions core/data/tests/excluded_by_target_os/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package proto

import "encoding/json"

// A struct with no target_os. Should be generated when
// we use --target-os.
type AlwaysAccept struct {
}
type DefinedTwice struct {
Field1 string `json:"field1"`
}
Expand All @@ -15,6 +19,11 @@ type NestedNotTarget1 struct {
}
type OtherExcluded struct {
}
type AlwaysAcceptEnum string
const (
AlwaysAcceptEnumVariant1 AlwaysAcceptEnum = "Variant1"
AlwaysAcceptEnumVariant2 AlwaysAcceptEnum = "Variant2"
)
type SomeEnum string
const (
)
Expand Down
13 changes: 13 additions & 0 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,11 @@ package com.agilebits.onepassword
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerialName

/// A struct with no target_os. Should be generated when
/// we use --target-os.
@Serializable
object AlwaysAccept

@Serializable
data class DefinedTwice (
val field1: String
Expand All @@ -23,6 +28,14 @@ object NestedNotTarget1
@Serializable
object OtherExcluded

@Serializable
enum class AlwaysAcceptEnum(val string: String) {
@SerialName("Variant1")
Variant1("Variant1"),
@SerialName("Variant2")
Variant2("Variant2"),
}

@Serializable
enum class SomeEnum(val string: String) {
}
Expand Down
16 changes: 16 additions & 0 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,10 @@ package com.agilebits

package onepassword {

// A struct with no target_os. Should be generated when
// we use --target-os.
class AlwaysAccept extends Serializable

case class DefinedTwice (
field1: String
)
Expand All @@ -16,6 +20,18 @@ class NestedNotTarget1 extends Serializable

class OtherExcluded extends Serializable

sealed trait AlwaysAcceptEnum {
def serialName: String
}
object AlwaysAcceptEnum {
case object Variant1 extends AlwaysAcceptEnum {
val serialName: String = "Variant1"
}
case object Variant2 extends AlwaysAcceptEnum {
val serialName: String = "Variant2"
}
}

sealed trait SomeEnum {
def serialName: String
}
Expand Down
11 changes: 11 additions & 0 deletions core/data/tests/excluded_by_target_os/output.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Foundation

/// A struct with no target_os. Should be generated when
/// we use --target-os.
public struct AlwaysAccept: Codable {
public init() {}
}

public struct DefinedTwice: Codable {
public let field1: String

Expand Down Expand Up @@ -28,6 +34,11 @@ public struct OtherExcluded: Codable {
public init() {}
}

public enum AlwaysAcceptEnum: String, Codable {
case variant1 = "Variant1"
case variant2 = "Variant2"
}

public enum SomeEnum: String, Codable {
}

Expand Down
12 changes: 12 additions & 0 deletions core/data/tests/excluded_by_target_os/output.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* A struct with no target_os. Should be generated when
* we use --target-os.
*/
export interface AlwaysAccept {
}

export interface DefinedTwice {
field1: string;
}
Expand All @@ -17,6 +24,11 @@ export interface NestedNotTarget1 {
export interface OtherExcluded {
}

export enum AlwaysAcceptEnum {
Variant1 = "Variant1",
Variant2 = "Variant2",
}

export enum SomeEnum {
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/language/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub type SortedCrateNames<'a> = &'a CrateName;
/// A sorted type name ref.
pub type SortedTypeNames<'a> = BTreeSet<&'a str>;

/// Refence types by crate that are scoped for a given output module.
/// Reference types by crate that are scoped for a given output module.
pub type ScopedCrateTypes<'a> = BTreeMap<SortedCrateNames<'a>, SortedTypeNames<'a>>;

/// All supported programming languages.
Expand Down
12 changes: 6 additions & 6 deletions core/src/language/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ pub struct Swift {
/// Whether or not to exclude the version header that normally appears at the top of generated code.
/// If you aren't generating a snapshot test, this setting can just be left as a default (false)
pub no_version_header: bool,
/// Are we generating mutliple modules?
/// Are we generating multiple modules?
pub multi_file: bool,
/// The contraints to apply to `CodableVoid`.
/// The constraints to apply to `CodableVoid`.
pub codablevoid_constraints: Vec<String>,
}

Expand Down Expand Up @@ -501,7 +501,7 @@ impl Language for Swift {
writeln!(w, "}}")
}

// TODO: This will be addded in the future.
// TODO: This will be added in the future.
// fn write_imports(
// &mut self,
// w: &mut dyn Write,
Expand Down Expand Up @@ -753,7 +753,7 @@ impl Swift {
.chain(self.default_decorators.iter().map(|s| s.as_str()))
}

/// When using mulitple file generation we write this into a separate module vs at the
/// When using multiple file generation we write this into a separate module vs at the
/// end of the generated file.
fn write_codable_file(&self, output_folder: &str) -> std::io::Result<()> {
let mut w = File::create(Path::new(output_folder).join("Codable.swift"))?;
Expand Down Expand Up @@ -789,7 +789,7 @@ impl Swift {
decorator_map: &'a DecoratorMap,
generic_types: &'a [String],
) -> String {
let swift_generic_contraints_annotated = decorator_map
let swift_generic_constraints_annotated = decorator_map
.get(&DecoratorKind::SwiftGenericConstraints)
.map(|generic_constraints| {
generic_constraints
Expand All @@ -814,7 +814,7 @@ impl Swift {
generic_types
.iter()
.map(
|type_name| match swift_generic_contraints_annotated.get(type_name.as_str()) {
|type_name| match swift_generic_constraints_annotated.get(type_name.as_str()) {
// Use constraints from swiftGenericConstraints decorator.
Some(constraints) => (type_name, Either::Left(constraints.iter().copied())),
// Use the default generic constraints if it is not part of a swiftGenericConstraints decorator.
Expand Down
2 changes: 1 addition & 1 deletion core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub enum ParseError {
/// Error with it's related data.
#[derive(Debug)]
pub struct ErrorInfo {
/// The crate where this error occured.
/// The crate where this error occurred.
pub crate_name: CrateName,
/// The file name being parsed.
pub file_name: String,
Expand Down
31 changes: 29 additions & 2 deletions core/src/target_os_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum TargetScope {
}

#[derive(Default)]
/// An iterator that yeilds all meta items and their contained scope.
/// An iterator that yields all meta items and their contained scope.
struct TargetOsIterator {
meta: Vec<(TargetScope, Meta)>,
}
Expand Down Expand Up @@ -82,6 +82,10 @@ impl Iterator for TargetOsIterator {
}

pub(crate) fn accept_target_os(attrs: &[Attribute], target_os: &[String]) -> bool {
if target_os.is_empty() {
return true;
}

let (accepted, rejected): (Vec<_>, Vec<_>) = attrs
.iter()
.inspect(|attr| {
Expand Down Expand Up @@ -124,7 +128,7 @@ mod test {
use flexi_logger::DeferredNow;
use log::Record;
use std::{io::Write, sync::Once};
use syn::{parse_quote, ItemStruct};
use syn::{parse_quote, ItemEnum, ItemStruct};

static INIT: Once = Once::new();

Expand Down Expand Up @@ -326,4 +330,27 @@ mod test {

assert!(accept_target_os(&test_struct.attrs, &["android".into()]))
}

#[test]
fn test_enum_no_target_os_enabled() {
init_log();

let test_enum: ItemEnum = parse_quote! {
#[typeshare]
pub enum TestEnum {
#[cfg(target_os = "ios")]
Variant1,
#[cfg(target_os = "android")]
Variant2,
}
};

let variants = test_enum
.variants
.iter()
.map(|v| accept_target_os(&v.attrs, &[]))
.collect::<Vec<_>>();

assert_eq!(&variants, &[true, true]);
}
}
6 changes: 3 additions & 3 deletions core/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'a> TypeShareVisitor<'a> {
self.parsed_data.import_types = diff;
}

/// Is this type annoted with at `#[cfg(target_os = "target")]` that does
/// Is this type annotated with at `#[cfg(target_os = "target")]` that does
/// not match `--target-os` argument?
#[inline(always)]
fn target_os_accepted(&self, attrs: &[Attribute]) -> bool {
Expand All @@ -217,8 +217,8 @@ impl<'ast, 'a> Visit<'ast> for TypeShareVisitor<'a> {
// field: some_module::RefType
// }
//
// vist_path would be after vist_item_use so we could retain imported module references
// and reconcile aftewards. visit_item_use would have to retain non type import types
// visit_path would be after visit_item_use so we could retain imported module references
// and reconcile afterwards. visit_item_use would have to retain non type import types
// which it discards right now.
//
let crate_candidate = p.segments.first()?.ident.to_string();
Expand Down

0 comments on commit 41de151

Please sign in to comment.