github.com/benma/gogen@v0.0.0-20160826115606-cf49914b915a/unmarshalmap/testpkg/simple_struct_unmarshalmap.go (about)

     1  /*
     2  * CODE GENERATED AUTOMATICALLY WITH github.com/ernesto-jimenez/gogen/unmarshalmap
     3  * THIS FILE SHOULD NOT BE EDITED BY HAND
     4   */
     5  
     6  package testpkg
     7  
     8  import (
     9  	"fmt"
    10  )
    11  
    12  // UnmarshalMap takes a map and unmarshals the fieds into the struct
    13  func (s *SimpleStruct) UnmarshalMap(m map[string]interface{}) error {
    14  
    15  	if v, ok := m["SimpleField"].(string); ok {
    16  		s.SimpleField = v
    17  
    18  	} else if v, exists := m["SimpleField"]; exists && v != nil {
    19  		return fmt.Errorf("expected field SimpleField to be string but got %T", m["SimpleField"])
    20  	}
    21  
    22  	if v, ok := m["field2"].(string); ok {
    23  		s.SimpleJSONTagged = v
    24  
    25  	} else if v, exists := m["field2"]; exists && v != nil {
    26  		return fmt.Errorf("expected field field2 to be string but got %T", m["field2"])
    27  	}
    28  
    29  	if v, ok := m["field3"].(string); ok {
    30  		s.SimpleJSONTaggedOmitted = v
    31  
    32  	} else if v, exists := m["field3"]; exists && v != nil {
    33  		return fmt.Errorf("expected field field3 to be string but got %T", m["field3"])
    34  	}
    35  
    36  	if v, ok := m["SimpleOmitEmptyNoName"].(string); ok {
    37  		s.SimpleOmitEmptyNoName = v
    38  
    39  	} else if v, exists := m["SimpleOmitEmptyNoName"]; exists && v != nil {
    40  		return fmt.Errorf("expected field SimpleOmitEmptyNoName to be string but got %T", m["SimpleOmitEmptyNoName"])
    41  	}
    42  
    43  	// Pointer SimplePointer
    44  	if p, ok := m["pointer"]; ok {
    45  
    46  		if m, ok := p.(string); ok {
    47  			s.SimplePointer = &m
    48  
    49  		} else if p == nil {
    50  			s.SimplePointer = nil
    51  		}
    52  
    53  	}
    54  
    55  	if v, ok := m["integer"].(int); ok {
    56  		s.SimpleInteger = v
    57  
    58  	} else if p, ok := m["integer"].(float64); ok {
    59  		v := int(p)
    60  		s.SimpleInteger = v
    61  
    62  	} else if v, exists := m["integer"]; exists && v != nil {
    63  		return fmt.Errorf("expected field integer to be int but got %T", m["integer"])
    64  	}
    65  
    66  	// Pointer SimpleIntegerPtr
    67  	if p, ok := m["integer_ptr"]; ok {
    68  
    69  		if m, ok := p.(int); ok {
    70  			s.SimpleIntegerPtr = &m
    71  
    72  		} else if m, ok := p.(float64); ok {
    73  			v := int(m)
    74  			s.SimpleIntegerPtr = &v
    75  
    76  		} else if p == nil {
    77  			s.SimpleIntegerPtr = nil
    78  		}
    79  
    80  	}
    81  
    82  	return nil
    83  }