-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from 1Password/jane/annotations/override-type
Parameter to override a field type on a per-language basis
- Loading branch information
Showing
13 changed files
with
338 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#[typeshare] | ||
#[serde(rename_all = "camelCase")] | ||
struct OverrideStruct { | ||
// These annotations are intentionally inconsistent across languages | ||
#[typeshare( | ||
swift(type = "Int"), | ||
typescript(readonly, type = "any | undefined"), | ||
kotlin(type = "Int"), go(type = "uint"), | ||
scala(type = "Short") | ||
)] | ||
field_to_override: String, | ||
} | ||
|
||
#[typeshare] | ||
#[serde(tag = "type", content = "content")] | ||
enum OverrideEnum { | ||
UnitVariant, | ||
TupleVariant(String), | ||
#[serde(rename_all = "camelCase")] | ||
AnonymousStructVariant { | ||
#[typeshare( | ||
swift(type = "Int"), | ||
typescript(readonly, type = "any | undefined"), | ||
kotlin(type = "Int"), go(type = "uint"), | ||
scala(type = "Short") | ||
)] | ||
field_to_override: String | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package proto | ||
|
||
import "encoding/json" | ||
|
||
type OverrideStruct struct { | ||
FieldToOverride uint `json:"fieldToOverride"` | ||
} | ||
// Generated type representing the anonymous struct variant `AnonymousStructVariant` of the `OverrideEnum` Rust enum | ||
type OverrideEnumAnonymousStructVariantInner struct { | ||
FieldToOverride uint `json:"fieldToOverride"` | ||
} | ||
type OverrideEnumTypes string | ||
const ( | ||
OverrideEnumTypeVariantUnitVariant OverrideEnumTypes = "UnitVariant" | ||
OverrideEnumTypeVariantTupleVariant OverrideEnumTypes = "TupleVariant" | ||
OverrideEnumTypeVariantAnonymousStructVariant OverrideEnumTypes = "AnonymousStructVariant" | ||
) | ||
type OverrideEnum struct{ | ||
Type OverrideEnumTypes `json:"type"` | ||
content interface{} | ||
} | ||
|
||
func (o *OverrideEnum) UnmarshalJSON(data []byte) error { | ||
var enum struct { | ||
Tag OverrideEnumTypes `json:"type"` | ||
Content json.RawMessage `json:"content"` | ||
} | ||
if err := json.Unmarshal(data, &enum); err != nil { | ||
return err | ||
} | ||
|
||
o.Type = enum.Tag | ||
switch o.Type { | ||
case OverrideEnumTypeVariantUnitVariant: | ||
return nil | ||
case OverrideEnumTypeVariantTupleVariant: | ||
var res string | ||
o.content = &res | ||
case OverrideEnumTypeVariantAnonymousStructVariant: | ||
var res OverrideEnumAnonymousStructVariantInner | ||
o.content = &res | ||
|
||
} | ||
if err := json.Unmarshal(enum.Content, &o.content); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o OverrideEnum) MarshalJSON() ([]byte, error) { | ||
var enum struct { | ||
Tag OverrideEnumTypes `json:"type"` | ||
Content interface{} `json:"content,omitempty"` | ||
} | ||
enum.Tag = o.Type | ||
enum.Content = o.content | ||
return json.Marshal(enum) | ||
} | ||
|
||
func (o OverrideEnum) TupleVariant() string { | ||
res, _ := o.content.(*string) | ||
return *res | ||
} | ||
func (o OverrideEnum) AnonymousStructVariant() *OverrideEnumAnonymousStructVariantInner { | ||
res, _ := o.content.(*OverrideEnumAnonymousStructVariantInner) | ||
return res | ||
} | ||
|
||
func NewOverrideEnumTypeVariantUnitVariant() OverrideEnum { | ||
return OverrideEnum{ | ||
Type: OverrideEnumTypeVariantUnitVariant, | ||
} | ||
} | ||
func NewOverrideEnumTypeVariantTupleVariant(content string) OverrideEnum { | ||
return OverrideEnum{ | ||
Type: OverrideEnumTypeVariantTupleVariant, | ||
content: &content, | ||
} | ||
} | ||
func NewOverrideEnumTypeVariantAnonymousStructVariant(content *OverrideEnumAnonymousStructVariantInner) OverrideEnum { | ||
return OverrideEnum{ | ||
Type: OverrideEnumTypeVariantAnonymousStructVariant, | ||
content: content, | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@file:NoLiveLiterals | ||
|
||
package com.agilebits.onepassword | ||
|
||
import androidx.compose.runtime.NoLiveLiterals | ||
import kotlinx.serialization.* | ||
|
||
@Serializable | ||
data class OverrideStruct ( | ||
val fieldToOverride: Int | ||
) | ||
|
||
/// Generated type representing the anonymous struct variant `AnonymousStructVariant` of the `OverrideEnum` Rust enum | ||
@Serializable | ||
data class OverrideEnumAnonymousStructVariantInner ( | ||
val fieldToOverride: Int | ||
) | ||
|
||
@Serializable | ||
sealed class OverrideEnum { | ||
@Serializable | ||
@SerialName("UnitVariant") | ||
object UnitVariant: OverrideEnum() | ||
@Serializable | ||
@SerialName("TupleVariant") | ||
data class TupleVariant(val content: String): OverrideEnum() | ||
@Serializable | ||
@SerialName("AnonymousStructVariant") | ||
data class AnonymousStructVariant(val content: OverrideEnumAnonymousStructVariantInner): OverrideEnum() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.agilebits | ||
|
||
package onepassword { | ||
|
||
case class OverrideStruct ( | ||
fieldToOverride: Short | ||
) | ||
|
||
// Generated type representing the anonymous struct variant `AnonymousStructVariant` of the `OverrideEnum` Rust enum | ||
case class OverrideEnumAnonymousStructVariantInner ( | ||
fieldToOverride: Short | ||
) | ||
|
||
sealed trait OverrideEnum { | ||
def serialName: String | ||
} | ||
object OverrideEnum { | ||
case object UnitVariant extends OverrideEnum { | ||
val serialName: String = "UnitVariant" | ||
} | ||
case class TupleVariant(content: String) extends OverrideEnum { | ||
val serialName: String = "TupleVariant" | ||
} | ||
case class AnonymousStructVariant(content: OverrideEnumAnonymousStructVariantInner) extends OverrideEnum { | ||
val serialName: String = "AnonymousStructVariant" | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import Foundation | ||
|
||
public struct OverrideStruct: Codable { | ||
public let fieldToOverride: Int | ||
|
||
public init(fieldToOverride: Int) { | ||
self.fieldToOverride = fieldToOverride | ||
} | ||
} | ||
|
||
|
||
/// Generated type representing the anonymous struct variant `AnonymousStructVariant` of the `OverrideEnum` Rust enum | ||
public struct OverrideEnumAnonymousStructVariantInner: Codable { | ||
public let fieldToOverride: Int | ||
|
||
public init(fieldToOverride: Int) { | ||
self.fieldToOverride = fieldToOverride | ||
} | ||
} | ||
public enum OverrideEnum: Codable { | ||
case unitVariant | ||
case tupleVariant(String) | ||
case anonymousStructVariant(OverrideEnumAnonymousStructVariantInner) | ||
|
||
enum CodingKeys: String, CodingKey, Codable { | ||
case unitVariant = "UnitVariant", | ||
tupleVariant = "TupleVariant", | ||
anonymousStructVariant = "AnonymousStructVariant" | ||
} | ||
|
||
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 .unitVariant: | ||
self = .unitVariant | ||
return | ||
case .tupleVariant: | ||
if let content = try? container.decode(String.self, forKey: .content) { | ||
self = .tupleVariant(content) | ||
return | ||
} | ||
case .anonymousStructVariant: | ||
if let content = try? container.decode(OverrideEnumAnonymousStructVariantInner.self, forKey: .content) { | ||
self = .anonymousStructVariant(content) | ||
return | ||
} | ||
} | ||
} | ||
throw DecodingError.typeMismatch(OverrideEnum.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for OverrideEnum")) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: ContainerCodingKeys.self) | ||
switch self { | ||
case .unitVariant: | ||
try container.encode(CodingKeys.unitVariant, forKey: .type) | ||
case .tupleVariant(let content): | ||
try container.encode(CodingKeys.tupleVariant, forKey: .type) | ||
try container.encode(content, forKey: .content) | ||
case .anonymousStructVariant(let content): | ||
try container.encode(CodingKeys.anonymousStructVariant, forKey: .type) | ||
try container.encode(content, forKey: .content) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface OverrideStruct { | ||
readonly fieldToOverride: any | undefined; | ||
} | ||
|
||
export type OverrideEnum = | ||
| { type: "UnitVariant", content?: undefined } | ||
| { type: "TupleVariant", content: string } | ||
| { type: "AnonymousStructVariant", content: { | ||
readonly fieldToOverride: any | undefined; | ||
}}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.