Skip to content

Commit

Permalink
Minor Fix/Feature: Ability to pass Thing to NewRecord(thing())
Browse files Browse the repository at this point in the history
  • Loading branch information
iDevelopThings committed Apr 16, 2023
1 parent fcfcda7 commit 49b63ec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion record.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ var (
// NewRecord creates a new Record[T] instance.
// Either the id or the value must be set.
func NewRecord[T any](value any) *Record[T] {
// Being lazy for now... not ideal though.. :|

if thing, ok := value.(Thing); ok {
return &Record[T]{
id: thing.Value(),
isSet: true,
kind: RecordTypeId,
}
}

// Being lazy for now... not ideal though.. :|
val := new(Record[T])

jsonVal, err := json.Marshal(value)
Expand Down
25 changes: 25 additions & 0 deletions record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,28 @@ func TestRecordCreation(t *testing.T) {
t.Error("Invalid value")
}
}
func TestCreatingRecordFromThing(t *testing.T) {
type subrec struct {
Id *Thing `json:"id"`
}
type rec struct {
Id *Thing `json:"id"`
Value *Record[subrec]
}

r := rec{
Id: NewThing("user:one"),
Value: NewRecord[subrec](NewThing("book:one")),
}

newRecord := NewRecord[subrec](r.Id)

if newRecord.IsId() == false {
t.Error("Invalid value")
}

if newRecord.Id() != "user:one" {
t.Error("Invalid value")
}

}

0 comments on commit 49b63ec

Please sign in to comment.