Skip to content

Commit

Permalink
Merge pull request #134 from 1Password/CerulanLumina/133-smart-pointers
Browse files Browse the repository at this point in the history
Support smart pointers
  • Loading branch information
snowsignal authored Oct 25, 2023
2 parents bcfa2c3 + baf8c1a commit c3ee2ad
Show file tree
Hide file tree
Showing 15 changed files with 455 additions and 188 deletions.
8 changes: 0 additions & 8 deletions core/data/tests/boxed_value/input.rs

This file was deleted.

75 changes: 0 additions & 75 deletions core/data/tests/boxed_value/output.go

This file was deleted.

21 changes: 0 additions & 21 deletions core/data/tests/boxed_value/output.kt

This file was deleted.

21 changes: 0 additions & 21 deletions core/data/tests/boxed_value/output.scala

This file was deleted.

51 changes: 0 additions & 51 deletions core/data/tests/boxed_value/output.swift

This file was deleted.

6 changes: 0 additions & 6 deletions core/data/tests/boxed_value/output.ts

This file was deleted.

56 changes: 56 additions & 0 deletions core/data/tests/smart_pointers/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// This is a comment.
#[typeshare]
#[serde(tag = "type", content = "content")]
pub enum BoxyColors {
Red,
Blue,
Green(Box<String>),
}

/// This is a comment.
#[typeshare]
#[serde(tag = "type", content = "content")]
pub struct ArcyColors {
pub red: Weak<u8>,
pub blue: ArcWeak<String>,
pub green: Arc<Vec<String>>,
}

/// This is a comment.
#[typeshare]
#[serde(tag = "type", content = "content")]
pub struct MutexyColors {
pub blue: Mutex<Vec<String>>,
pub green: Mutex<String>,
}

/// This is a comment.
#[typeshare]
#[serde(tag = "type", content = "content")]
pub struct RcyColors {
pub red: RcWeak<String>,
pub blue: Rc<Vec<String>>,
pub green: Rc<String>,
}

/// This is a comment.
#[typeshare]
#[serde(tag = "type", content = "content")]
pub struct CellyColors {
pub red: Cell<String>,
pub blue: RefCell<Vec<String>>,
}

/// This is a comment.
#[typeshare]
#[serde(tag = "type", content = "content")]
pub struct LockyColors {
pub red: RwLock<String>,
}

/// This is a comment.
#[typeshare]
#[serde(tag = "type", content = "content")]
pub struct CowyColors<'a> {
pub lifetime: Cow<'a, str>,
}
105 changes: 105 additions & 0 deletions core/data/tests/smart_pointers/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package proto

import "encoding/json"

// This is a comment.
type ArcyColors struct {
Red int `json:"red"`
Blue string `json:"blue"`
Green []string `json:"green"`
}
// This is a comment.
type MutexyColors struct {
Blue []string `json:"blue"`
Green string `json:"green"`
}
// This is a comment.
type RcyColors struct {
Red string `json:"red"`
Blue []string `json:"blue"`
Green string `json:"green"`
}
// This is a comment.
type CellyColors struct {
Red string `json:"red"`
Blue []string `json:"blue"`
}
// This is a comment.
type LockyColors struct {
Red string `json:"red"`
}
// This is a comment.
type CowyColors struct {
Lifetime string `json:"lifetime"`
}
// This is a comment.
type BoxyColorsTypes string
const (
BoxyColorsTypeVariantRed BoxyColorsTypes = "Red"
BoxyColorsTypeVariantBlue BoxyColorsTypes = "Blue"
BoxyColorsTypeVariantGreen BoxyColorsTypes = "Green"
)
type BoxyColors struct{
Type BoxyColorsTypes `json:"type"`
content interface{}
}

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

b.Type = enum.Tag
switch b.Type {
case BoxyColorsTypeVariantRed:
return nil
case BoxyColorsTypeVariantBlue:
return nil
case BoxyColorsTypeVariantGreen:
var res string
b.content = &res

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

return nil
}

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

func (b BoxyColors) Green() string {
res, _ := b.content.(*string)
return *res
}

func NewBoxyColorsTypeVariantRed() BoxyColors {
return BoxyColors{
Type: BoxyColorsTypeVariantRed,
}
}
func NewBoxyColorsTypeVariantBlue() BoxyColors {
return BoxyColors{
Type: BoxyColorsTypeVariantBlue,
}
}
func NewBoxyColorsTypeVariantGreen(content string) BoxyColors {
return BoxyColors{
Type: BoxyColorsTypeVariantGreen,
content: &content,
}
}

Loading

0 comments on commit c3ee2ad

Please sign in to comment.