Skip to content

Commit

Permalink
replace directive (#346)
Browse files Browse the repository at this point in the history
Adds a `replace` directive that makes it easier to serialize foreign types.

Example usage with github.com/google/uuid

  ```go
  package main

  import "github.com/google/uuid"

  //go:generate msgp

  //msgp:replace uuid.UUID with:UUID
  type UUID [16]byte

  // Or like that
  //msgp:replace uuid.UUID with:[16]byte

  type User struct {
    ID uuid.UUID
  }
  ```
  • Loading branch information
infastin committed Jul 2, 2024
1 parent 7de405e commit 2ad0d1b
Show file tree
Hide file tree
Showing 8 changed files with 498 additions and 30 deletions.
76 changes: 76 additions & 0 deletions _generated/replace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package _generated

//go:generate msgp
//msgp:replace Any with:any
//msgp:replace MapString with:CompatibleMapString
//msgp:replace MapAny with:map[string]any
//msgp:replace SliceString with:[]string
//msgp:replace SliceInt with:CompatibleSliceInt
//msgp:replace Array8 with:CompatibleArray8
//msgp:replace Array16 with:[16]byte
//msgp:replace String with:string
//msgp:replace Int with:CompatibleInt
//msgp:replace Uint with:uint
//msgp:replace Float32 with:CompatibleFloat32
//msgp:replace Float64 with:CompatibleFloat64
//msgp:replace Time with:time.Time
//msgp:replace Duration with:time.Duration
//msgp:replace StructA with:CompatibleStructA
//msgp:replace StructB with:CompatibleStructB
//msgp:replace StructC with:CompatibleStructC
//msgp:replace StructD with:CompatibleStructD
//msgp:replace StructI with:CompatibleStructI
//msgp:replace StructS with:CompatibleStructS

type (
CompatibleMapString map[string]string
CompatibleArray8 [8]byte
CompatibleInt int
CompatibleFloat32 float32
CompatibleFloat64 float64
CompatibleSliceInt []Int

// Doesn't work
// CompatibleTime time.Time

CompatibleStructA struct {
StructB StructB
Int Int
}

CompatibleStructB struct {
StructC StructC
Any Any
Array8 Array8
}

CompatibleStructC struct {
StructD StructD
Float64 Float32
Float32 Float64
}

CompatibleStructD struct {
Time Time
Duration Duration
MapString MapString
}

CompatibleStructI struct {
Int *Int
Uint *Uint
}

CompatibleStructS struct {
Slice SliceInt
}

Dummy struct {
StructA StructA
StructI StructI
StructS StructS
Array16 Array16
Uint Uint
String String
}
)
54 changes: 54 additions & 0 deletions _generated/replace_ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package _generated

import "time"

// external types to test replace directive

type (
MapString map[string]string
MapAny map[string]any
SliceString []String
SliceInt []Int
Array8 [8]byte
Array16 [16]byte
Int int
Uint uint
String string
Float32 float32
Float64 float64
Time time.Time
Duration time.Duration
Any any

StructA struct {
StructB StructB
Int Int
}

StructB struct {
StructC StructC
Any Any
Array8 Array8
}

StructC struct {
StructD StructD
Float64 Float32
Float32 Float64
}

StructD struct {
Time Time
Duration Duration
MapString MapString
}

StructI struct {
Int *Int
Uint *Uint
}

StructS struct {
Slice SliceInt
}
)
Loading

0 comments on commit 2ad0d1b

Please sign in to comment.