Skip to content

Commit

Permalink
Fix up unmarshalFunc generation for interface types
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed May 6, 2024
1 parent 17344a8 commit 6e83d72
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 12 deletions.
69 changes: 69 additions & 0 deletions gen_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,75 @@ func (v MergedBackgroundFill) MergeBackgroundFill() MergedBackgroundFill {
return v
}

// unmarshalBackgroundFillArray is a JSON unmarshalling helper which allows unmarshalling an array of interfaces
// using unmarshalBackgroundFill.
func unmarshalBackgroundFillArray(d json.RawMessage) ([]BackgroundFill, error) {
if len(d) == 0 {
return nil, nil
}

var ds []json.RawMessage
err := json.Unmarshal(d, &ds)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal initial BackgroundFill JSON into an array: %w", err)
}

var vs []BackgroundFill
for idx, d := range ds {
v, err := unmarshalBackgroundFill(d)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal BackgroundFill on array item %d: %w", idx, err)
}
vs = append(vs, v)
}

return vs, nil
}

// unmarshalBackgroundFill is a JSON unmarshal helper to marshal the right structs into a BackgroundFill interface
// based on the Type field.
func unmarshalBackgroundFill(d json.RawMessage) (BackgroundFill, error) {
if len(d) == 0 {
return nil, nil
}

t := struct {
Type string
}{}
err := json.Unmarshal(d, &t)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal BackgroundFill for constant field 'Type': %w", err)
}

switch t.Type {
case "solid":
s := BackgroundFillSolid{}
err := json.Unmarshal(d, &s)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal BackgroundFill for value 'solid': %w", err)
}
return s, nil

case "gradient":
s := BackgroundFillGradient{}
err := json.Unmarshal(d, &s)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal BackgroundFill for value 'gradient': %w", err)
}
return s, nil

case "freeform_gradient":
s := BackgroundFillFreeformGradient{}
err := json.Unmarshal(d, &s)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal BackgroundFill for value 'freeform_gradient': %w", err)
}
return s, nil

}
return nil, fmt.Errorf("unknown interface for BackgroundFill with Type %v", t.Type)
}

// BackgroundFillFreeformGradient (https://core.telegram.org/bots/api#backgroundfillfreeformgradient)
//
// The background is a freeform gradient that rotates after every message in the chat.
Expand Down
37 changes: 25 additions & 12 deletions scripts/generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (td TypeDescription) sentByAPI(d APIDescription) bool {
continue
}

if usesChildType(d, child, td.Name, []string{td.Name}) {
if child.usesChildType(d, td.Name, []string{td.Name}) {
return true
}
checked[r] = true
Expand All @@ -65,26 +65,39 @@ func (td TypeDescription) sentByAPI(d APIDescription) bool {
return false
}

func usesChildType(d APIDescription, tgType TypeDescription, typeName string, skip []string) bool {
for _, f := range tgType.Fields {
func (td TypeDescription) usesChildType(d APIDescription, typeName string, skip []string) bool {
for _, f := range td.Fields {
for _, t := range f.Types {
if isTgArray(t) {
t = strings.TrimPrefix(t, "Array of ")
}

if t == typeName {
if td.isChildType(d, t, typeName, skip) {
return true
}
}
}

if contains(t, skip) {
continue
}
for _, t := range td.Subtypes {
if td.isChildType(d, t, typeName, skip) {
return true
}
}
return false
}

if child, ok := d.Types[t]; ok && t != tgType.Name {
if usesChildType(d, child, typeName, append(skip, tgType.Name)) {
return true
}
}
func (td TypeDescription) isChildType(d APIDescription, t string, typeName string, skip []string) bool {
if t == typeName {
return true
}

if contains(t, skip) {
return false
}

if child, ok := d.Types[t]; ok && t != td.Name {
if child.usesChildType(d, typeName, append(skip, td.Name)) {
return true
}
}
return false
Expand Down

0 comments on commit 6e83d72

Please sign in to comment.