Skip to content

Commit

Permalink
Merge pull request #172 from darrell-roberts/fix-typeshare-skip
Browse files Browse the repository at this point in the history
Fix typeshare skip
  • Loading branch information
kareid authored May 17, 2024
2 parents 360604c + 838fb0d commit 0367431
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Version 1.10.0-beta

## 1.10.0-beta.1
## 1.10.0-beta.2

Fixed a bug involving type aliases.

Expand Down Expand Up @@ -36,7 +36,7 @@ This release brings support for various Rust std smart pointers, as well as a CL

- Added support for various Rust std smart pointers. [#134](https://github.com/1Password/typeshare/pull/134)
- Added CLI flag to opt-into following symbolic links. [#156](https://github.com/1Password/typeshare/pull/156)
- Migrate to syn version 2.0. [#130](https://github.com/1Password/typeshare/pull/130)
- Migrate to syn version 2.0. [#130](https://github.com/1Password/typeshare/pull/130)

### Community contributors

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typeshare-cli"
version = "1.10.0-beta.1"
version = "1.10.0-beta.2"
edition = "2021"
description = "Command Line Tool for generating language files with typeshare"
license = "MIT OR Apache-2.0"
Expand All @@ -22,5 +22,5 @@ once_cell = "1"
rayon = "1.10"
serde = { version = "1", features = ["derive"] }
toml = "0.8"
typeshare-core = { path = "../core", version = "1.10.0-beta.1" }
typeshare-core = { path = "../core", version = "1.10.0-beta.2" }
anyhow = "1"
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typeshare-core"
version = "1.10.0-beta.1"
version = "1.10.0-beta.2"
license = "MIT OR Apache-2.0"
edition = "2021"
description = "The code generator used by Typeshare's command line tool"
Expand All @@ -17,6 +17,6 @@ joinery = "2"

[dev-dependencies]
anyhow = "1"
expect-test = "1.1"
expect-test = "1.5"
once_cell = "1"
cool_asserts = "2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Enum keeping track of who autofilled a field
#[typeshare]
#[serde(tag = "type", content = "content")]
pub enum AutofilledBy {
/// This field was autofilled by us
Us {
/// The UUID for the fill
uuid: String,
},
/// Something else autofilled this field
SomethingElse {
/// The UUID for the fill
uuid: String,
/// Some other thing
#[typeshare(skip)]
thing: i32,
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package proto

import "encoding/json"

// Generated type representing the anonymous struct variant `Us` of the `AutofilledBy` Rust enum
type AutofilledByUsInner struct {
// The UUID for the fill
Uuid string `json:"uuid"`
}
// Generated type representing the anonymous struct variant `SomethingElse` of the `AutofilledBy` Rust enum
type AutofilledBySomethingElseInner struct {
// The UUID for the fill
Uuid string `json:"uuid"`
}
// Enum keeping track of who autofilled a field
type AutofilledByTypes string
const (
// This field was autofilled by us
AutofilledByTypeVariantUs AutofilledByTypes = "Us"
// Something else autofilled this field
AutofilledByTypeVariantSomethingElse AutofilledByTypes = "SomethingElse"
)
type AutofilledBy struct{
Type AutofilledByTypes `json:"type"`
content interface{}
}

func (a *AutofilledBy) UnmarshalJSON(data []byte) error {
var enum struct {
Tag AutofilledByTypes `json:"type"`
Content json.RawMessage `json:"content"`
}
if err := json.Unmarshal(data, &enum); err != nil {
return err
}

a.Type = enum.Tag
switch a.Type {
case AutofilledByTypeVariantUs:
var res AutofilledByUsInner
a.content = &res
case AutofilledByTypeVariantSomethingElse:
var res AutofilledBySomethingElseInner
a.content = &res

}
if err := json.Unmarshal(enum.Content, &a.content); err != nil {
return err
}

return nil
}

func (a AutofilledBy) MarshalJSON() ([]byte, error) {
var enum struct {
Tag AutofilledByTypes `json:"type"`
Content interface{} `json:"content,omitempty"`
}
enum.Tag = a.Type
enum.Content = a.content
return json.Marshal(enum)
}

func (a AutofilledBy) Us() *AutofilledByUsInner {
res, _ := a.content.(*AutofilledByUsInner)
return res
}
func (a AutofilledBy) SomethingElse() *AutofilledBySomethingElseInner {
res, _ := a.content.(*AutofilledBySomethingElseInner)
return res
}

func NewAutofilledByTypeVariantUs(content *AutofilledByUsInner) AutofilledBy {
return AutofilledBy{
Type: AutofilledByTypeVariantUs,
content: content,
}
}
func NewAutofilledByTypeVariantSomethingElse(content *AutofilledBySomethingElseInner) AutofilledBy {
return AutofilledBy{
Type: AutofilledByTypeVariantSomethingElse,
content: content,
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.agilebits.onepassword

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

/// Generated type representing the anonymous struct variant `Us` of the `AutofilledBy` Rust enum
@Serializable
data class AutofilledByUsInner (
/// The UUID for the fill
val uuid: String
)

/// Generated type representing the anonymous struct variant `SomethingElse` of the `AutofilledBy` Rust enum
@Serializable
data class AutofilledBySomethingElseInner (
/// The UUID for the fill
val uuid: String
)

/// Enum keeping track of who autofilled a field
@Serializable
sealed class AutofilledBy {
/// This field was autofilled by us
@Serializable
@SerialName("Us")
data class Us(val content: AutofilledByUsInner): AutofilledBy()
/// Something else autofilled this field
@Serializable
@SerialName("SomethingElse")
data class SomethingElse(val content: AutofilledBySomethingElseInner): AutofilledBy()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.agilebits

package onepassword {

// Generated type representing the anonymous struct variant `Us` of the `AutofilledBy` Rust enum
case class AutofilledByUsInner (
// The UUID for the fill
uuid: String
)

// Generated type representing the anonymous struct variant `SomethingElse` of the `AutofilledBy` Rust enum
case class AutofilledBySomethingElseInner (
// The UUID for the fill
uuid: String
)

// Enum keeping track of who autofilled a field
sealed trait AutofilledBy {
def serialName: String
}
object AutofilledBy {
// This field was autofilled by us
case class Us(content: AutofilledByUsInner) extends AutofilledBy {
val serialName: String = "Us"
}
// Something else autofilled this field
case class SomethingElse(content: AutofilledBySomethingElseInner) extends AutofilledBy {
val serialName: String = "SomethingElse"
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation


/// Generated type representing the anonymous struct variant `Us` of the `AutofilledBy` Rust enum
public struct AutofilledByUsInner: Codable {
/// The UUID for the fill
public let uuid: String

public init(uuid: String) {
self.uuid = uuid
}
}

/// Generated type representing the anonymous struct variant `SomethingElse` of the `AutofilledBy` Rust enum
public struct AutofilledBySomethingElseInner: Codable {
/// The UUID for the fill
public let uuid: String

public init(uuid: String) {
self.uuid = uuid
}
}
/// Enum keeping track of who autofilled a field
public enum AutofilledBy: Codable {
/// This field was autofilled by us
case us(AutofilledByUsInner)
/// Something else autofilled this field
case somethingElse(AutofilledBySomethingElseInner)

enum CodingKeys: String, CodingKey, Codable {
case us = "Us",
somethingElse = "SomethingElse"
}

private enum ContainerCodingKeys: String, CodingKey {
case type, content
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: ContainerCodingKeys.self)
if let type = try? container.decode(CodingKeys.self, forKey: .type) {
switch type {
case .us:
if let content = try? container.decode(AutofilledByUsInner.self, forKey: .content) {
self = .us(content)
return
}
case .somethingElse:
if let content = try? container.decode(AutofilledBySomethingElseInner.self, forKey: .content) {
self = .somethingElse(content)
return
}
}
}
throw DecodingError.typeMismatch(AutofilledBy.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for AutofilledBy"))
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: ContainerCodingKeys.self)
switch self {
case .us(let content):
try container.encode(CodingKeys.us, forKey: .type)
try container.encode(content, forKey: .content)
case .somethingElse(let content):
try container.encode(CodingKeys.somethingElse, forKey: .type)
try container.encode(content, forKey: .content)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** Enum keeping track of who autofilled a field */
export type AutofilledBy =
/** This field was autofilled by us */
| { type: "Us", content: {
/** The UUID for the fill */
uuid: string;
}}
/** Something else autofilled this field */
| { type: "SomethingElse", content: {
/** The UUID for the fill */
uuid: string;
}};

1 change: 1 addition & 0 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ fn parse_enum_variant(
fields: fields_named
.named
.iter()
.filter(|f| !is_skipped(&f.attrs))
.map(|f| {
let field_type = if let Some(ty) = get_field_type_override(&f.attrs) {
ty.parse()?
Expand Down
1 change: 1 addition & 0 deletions core/tests/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,5 @@ tests! {
scala,
go
];
can_generate_anonymous_struct_with_skipped_fields: [swift, kotlin, scala, typescript, go];
}
6 changes: 3 additions & 3 deletions flake.lock

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

0 comments on commit 0367431

Please sign in to comment.