github.com/jfcote87/salesforce@v0.1.0/genpkgs/example_test.go (about)

     1  // Copyright 2022 James Cote
     2  // All rights reserved.
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package genpkgs_test
     7  
     8  import (
     9  	"context"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  
    14  	"github.com/jfcote87/oauth2"
    15  	"github.com/jfcote87/salesforce"
    16  	"github.com/jfcote87/salesforce/genpkgs"
    17  )
    18  
    19  var config = &genpkgs.Config{
    20  	StructOverrides: map[string]*genpkgs.Override{
    21  		"Account": {
    22  			Name: "Household",
    23  			Fields: map[string]genpkgs.FldOverride{
    24  				"AccountNumber": {
    25  					Name: "HouseholdID",
    26  				},
    27  			},
    28  		},
    29  	},
    30  	SkipRelationshipGlobal: map[string]bool{
    31  		"CreatedById":      true,
    32  		"LastModifiedById": true,
    33  	},
    34  	Packages: []genpkgs.Parameters{
    35  		{
    36  			Name:            "content",
    37  			Description:     "defines all standard content sobjects",
    38  			GoFilename:      "content/content.go",
    39  			IncludeStandard: true,
    40  			IncludeMatch:    "^Content",
    41  			ReplaceMatch:    "^Content",
    42  			ReplaceWith:     "",
    43  		},
    44  		{
    45  			Name:                   "changes",
    46  			Description:            "defines all ChangeEvent sobjects",
    47  			GoFilename:             "changes/changes.go",
    48  			IncludeStandard:        true,
    49  			IncludeCustom:          true,
    50  			AssociatedIdentityType: "ChangeEvent",
    51  		},
    52  		{
    53  			Name:          "custom",
    54  			Description:   "describes all custom sobjects",
    55  			GoFilename:    "custom/custom.go",
    56  			IncludeCustom: true,
    57  		},
    58  		{
    59  			Name:            "standard",
    60  			Description:     "describes all standard sobjects",
    61  			GoFilename:      "sobjects.go",
    62  			IncludeStandard: true,
    63  		},
    64  	},
    65  }
    66  
    67  var validOauth2Token = &oauth2.Token{
    68  	AccessToken: "Valid Token",
    69  }
    70  
    71  var packagePath = "gowork/sf/sobjects"
    72  
    73  func WritePackages() {
    74  	ctx := context.Background()
    75  
    76  	if err := os.Chdir(packagePath); err != nil {
    77  		log.Fatalf("%v", err)
    78  	}
    79  
    80  	var instanceName = "my-instance-name"
    81  	sv := salesforce.New(instanceName, "", oauth2.StaticTokenSource(validOauth2Token))
    82  
    83  	// MakeSource returns a map of [file name] and a byte array of go source
    84  	srcMap, err := config.MakeSource(ctx, sv, nil)
    85  	if err != nil {
    86  		log.Fatalf("%v", err)
    87  	}
    88  	for fn, src := range srcMap {
    89  		if err := ioutil.WriteFile(fn, src, 0755); err != nil {
    90  			log.Printf("error writing %s %v", fn, err)
    91  		}
    92  	}
    93  }