Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
implementing bson marshalling interface for value set generators to
Browse files Browse the repository at this point in the history
import/export code as string vs iota
  • Loading branch information
stat committed Sep 1, 2023
1 parent 7a918d9 commit b380eb1
Show file tree
Hide file tree
Showing 72 changed files with 1,387 additions and 114 deletions.
38 changes: 36 additions & 2 deletions fhir-models-gen/cmd/valueSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"errors"
"fmt"
"strings"

"github.com/dave/jennifer/jen"
"github.com/samply/golang-fhir-models/fhir-models-gen/fhir"
"strings"
)

func generateValueSet(resources ResourceMap, valueSet fhir.ValueSet) (*jen.File, error) {
Expand Down Expand Up @@ -50,9 +51,42 @@ func generateValueSet(resources ResourceMap, valueSet fhir.ValueSet) (*jen.File,
file.Type().Id(*valueSet.Name).Int()
file.Const().DefsFunc(constsRoot(*valueSet.Name, codeSystem.Concept))

file.Func().
Params(jen.Id("code").Op("*").Id(*valueSet.Name)).
Id("MarshalBSONValue").
Params().
Params(jen.Id("bsontype.Type"), jen.Op("[]").Byte(), jen.Error()).
Block(
jen.Return(jen.Qual("go.mongodb.org/mongo-driver/bson", "MarshalValue").Call(jen.Id("code").Op(".").Id("Code").Call())),
)

file.Func().
Params(jen.Id("code").Op("*").Id(*valueSet.Name)).
Id("UnmarshalBSONValue").
Params(jen.Id("t").Qual("go.mongodb.org/mongo-driver/bson/bsontype", "Type"), jen.Id("bytes").Op("[]").Byte()).
Params(jen.Error()).
Block(
jen.If(jen.Id("t").Op("!=").Id("bsontype.String")).Block(
jen.Id("err").Op(":=").Qual("fmt", "Errorf").Call(jen.Lit("UnmarshalBSONValue error: cannot unmarshal non string value")),
jen.Return(jen.Id("err")),
),
jen.Id("reader").Op(":=").Qual("go.mongodb.org/mongo-driver/bson/bsonrw", "NewBSONValueReader").Call(jen.Id("t"), jen.Id("bytes")),
jen.Id("decoder").Op(",").Id("err").Op(":=").Qual("go.mongodb.org/mongo-driver/bson", "NewDecoder").Call(jen.Id("reader")),
jen.If(jen.Id("err").Op("!=").Nil()).Block(
jen.Return(jen.Id("err")),
),
jen.Var().Id("s").String(),
jen.Id("err").Op("=").Id("decoder").Op(".").Id("Decode").Call(jen.Op("&").Id("s")),
jen.If(jen.Id("err").Op("!=").Nil()).Block(
jen.Return(jen.Id("err")),
),
jen.Switch(jen.Id("s")).BlockFunc(unmarshalRoot(*valueSet.Name, codeSystem.Concept)),
jen.Return(jen.Id("nil")),
)

// MarshalJSON function
file.Func().
Params(jen.Id("code").Id(*valueSet.Name)).
Params(jen.Id("code").Op("*").Id(*valueSet.Name)).
Id("MarshalJSON").
Params().
Params(jen.Op("[]").Byte(), jen.Error()).
Expand Down
2 changes: 1 addition & 1 deletion fhir-models/fhir/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package fhir

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// Address is documented here http://hl7.org/fhir/StructureDefinition/Address
Expand Down
37 changes: 35 additions & 2 deletions fhir-models/fhir/addressType.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package fhir
import (
"encoding/json"
"fmt"
bson "go.mongodb.org/mongo-driver/bson"
bsonrw "go.mongodb.org/mongo-driver/bson/bsonrw"
bsontype "go.mongodb.org/mongo-driver/bson/bsontype"
"strings"
)

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// AddressType is documented here http://hl7.org/fhir/ValueSet/address-type
Expand All @@ -32,7 +35,37 @@ const (
AddressTypeBoth
)

func (code *AddressType)MarshalJSON() ([]byte, error) {
func (code *AddressType) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(code.Code())
}
func (code *AddressType) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error {
if t != bsontype.String {
err := fmt.Errorf("UnmarshalBSONValue error: cannot unmarshal non string value")
return err
}
reader := bsonrw.NewBSONValueReader(t, bytes)
decoder, err := bson.NewDecoder(reader)
if err != nil {
return err
}
var s string
err = decoder.Decode(&s)
if err != nil {
return err
}
switch s {
case "postal":
*code = AddressTypePostal
case "physical":
*code = AddressTypePhysical
case "both":
*code = AddressTypeBoth
default:
return fmt.Errorf("unknown AddressType code `%s`", s)
}
return nil
}
func (code *AddressType) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
}
func (code *AddressType) UnmarshalJSON(json []byte) error {
Expand Down
41 changes: 39 additions & 2 deletions fhir-models/fhir/addressUse.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package fhir
import (
"encoding/json"
"fmt"
bson "go.mongodb.org/mongo-driver/bson"
bsonrw "go.mongodb.org/mongo-driver/bson/bsonrw"
bsontype "go.mongodb.org/mongo-driver/bson/bsontype"
"strings"
)

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// AddressUse is documented here http://hl7.org/fhir/ValueSet/address-use
Expand All @@ -34,7 +37,41 @@ const (
AddressUseBilling
)

func (code *AddressUse)MarshalJSON() ([]byte, error) {
func (code *AddressUse) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(code.Code())
}
func (code *AddressUse) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error {
if t != bsontype.String {
err := fmt.Errorf("UnmarshalBSONValue error: cannot unmarshal non string value")
return err
}
reader := bsonrw.NewBSONValueReader(t, bytes)
decoder, err := bson.NewDecoder(reader)
if err != nil {
return err
}
var s string
err = decoder.Decode(&s)
if err != nil {
return err
}
switch s {
case "home":
*code = AddressUseHome
case "work":
*code = AddressUseWork
case "temp":
*code = AddressUseTemp
case "old":
*code = AddressUseOld
case "billing":
*code = AddressUseBilling
default:
return fmt.Errorf("unknown AddressUse code `%s`", s)
}
return nil
}
func (code *AddressUse) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
}
func (code *AddressUse) UnmarshalJSON(json []byte) error {
Expand Down
2 changes: 1 addition & 1 deletion fhir-models/fhir/age.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package fhir

import "encoding/json"

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// Age is documented here http://hl7.org/fhir/StructureDefinition/Age
Expand Down
37 changes: 35 additions & 2 deletions fhir-models/fhir/aggregationMode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package fhir
import (
"encoding/json"
"fmt"
bson "go.mongodb.org/mongo-driver/bson"
bsonrw "go.mongodb.org/mongo-driver/bson/bsonrw"
bsontype "go.mongodb.org/mongo-driver/bson/bsontype"
"strings"
)

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// AggregationMode is documented here http://hl7.org/fhir/ValueSet/resource-aggregation-mode
Expand All @@ -32,7 +35,37 @@ const (
AggregationModeBundled
)

func (code *AggregationMode)MarshalJSON() ([]byte, error) {
func (code *AggregationMode) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(code.Code())
}
func (code *AggregationMode) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error {
if t != bsontype.String {
err := fmt.Errorf("UnmarshalBSONValue error: cannot unmarshal non string value")
return err
}
reader := bsonrw.NewBSONValueReader(t, bytes)
decoder, err := bson.NewDecoder(reader)
if err != nil {
return err
}
var s string
err = decoder.Decode(&s)
if err != nil {
return err
}
switch s {
case "contained":
*code = AggregationModeContained
case "referenced":
*code = AggregationModeReferenced
case "bundled":
*code = AggregationModeBundled
default:
return fmt.Errorf("unknown AggregationMode code `%s`", s)
}
return nil
}
func (code *AggregationMode) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
}
func (code *AggregationMode) UnmarshalJSON(json []byte) error {
Expand Down
2 changes: 1 addition & 1 deletion fhir-models/fhir/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package fhir

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// Annotation is documented here http://hl7.org/fhir/StructureDefinition/Annotation
Expand Down
2 changes: 1 addition & 1 deletion fhir-models/fhir/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package fhir

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// Attachment is documented here http://hl7.org/fhir/StructureDefinition/Attachment
Expand Down
39 changes: 37 additions & 2 deletions fhir-models/fhir/bindingStrength.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ package fhir
import (
"encoding/json"
"fmt"
bson "go.mongodb.org/mongo-driver/bson"
bsonrw "go.mongodb.org/mongo-driver/bson/bsonrw"
bsontype "go.mongodb.org/mongo-driver/bson/bsontype"
"strings"
)

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// BindingStrength is documented here http://hl7.org/fhir/ValueSet/binding-strength
Expand All @@ -33,7 +36,39 @@ const (
BindingStrengthExample
)

func (code *BindingStrength)MarshalJSON() ([]byte, error) {
func (code *BindingStrength) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(code.Code())
}
func (code *BindingStrength) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error {
if t != bsontype.String {
err := fmt.Errorf("UnmarshalBSONValue error: cannot unmarshal non string value")
return err
}
reader := bsonrw.NewBSONValueReader(t, bytes)
decoder, err := bson.NewDecoder(reader)
if err != nil {
return err
}
var s string
err = decoder.Decode(&s)
if err != nil {
return err
}
switch s {
case "required":
*code = BindingStrengthRequired
case "extensible":
*code = BindingStrengthExtensible
case "preferred":
*code = BindingStrengthPreferred
case "example":
*code = BindingStrengthExample
default:
return fmt.Errorf("unknown BindingStrength code `%s`", s)
}
return nil
}
func (code *BindingStrength) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
}
func (code *BindingStrength) UnmarshalJSON(json []byte) error {
Expand Down
6 changes: 3 additions & 3 deletions fhir-models/fhir/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package fhir

import "encoding/json"

// THIS FILE IS GENERATED BY https://github.com/stat/golang-fhir-models
// THIS FILE IS GENERATED BY https://github.com/samply/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND

// Bundle is documented here http://hl7.org/fhir/StructureDefinition/Bundle
Expand Down Expand Up @@ -82,12 +82,12 @@ type BundleEntryResponse struct {
type OtherBundle Bundle

// MarshalJSON marshals the given Bundle as JSON into a byte slice
func (r *Bundle)MarshalJSON() ([]byte, error) {
func (r Bundle) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
OtherBundle
ResourceType string `json:"resourceType"`
}{
OtherBundle: OtherBundle(*r),
OtherBundle: OtherBundle(r),
ResourceType: "Bundle",
})
}
Expand Down
Loading

0 comments on commit b380eb1

Please sign in to comment.